|
| 1 | +.. _laravel-schema-builder: |
| 2 | + |
| 3 | +============== |
| 4 | +Schema Builder |
| 5 | +============== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: tutorial |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: php framework, odm, code example, schema facade, eloquent, blueprint, artisan, migrate |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +Laravel provides a **facade** to access the schema builder class ``Schema``, |
| 24 | +which lets you create and modify tables. Facades are static interfaces to |
| 25 | +classes that make the syntax more concise and improve testability. |
| 26 | + |
| 27 | +{+odm-short+} supports a subset of the index and collection management methods |
| 28 | +in the Laravel ``Schema`` facade. |
| 29 | + |
| 30 | +To learn more about facades, see `Facades <https://laravel.com/docs/{+laravel-docs-version+}/facades>`__ |
| 31 | +in the Laravel documentation. |
| 32 | + |
| 33 | +The following sections describe the Laravel schema builder features available |
| 34 | +in {+odm-short+} and show examples of how to use them: |
| 35 | + |
| 36 | +- :ref:`<laravel-eloquent-migrations>` |
| 37 | +- :ref:`<laravel-eloquent-collection-exists>` |
| 38 | +- :ref:`<laravel-eloquent-indexes>` |
| 39 | + |
| 40 | +.. note:: |
| 41 | + |
| 42 | + {+odm-short+} supports managing indexes and collections, but |
| 43 | + excludes support for MongoDB JSON schemas for data validation. To learn |
| 44 | + more about JSON schema validation, see :manual:`Schema Validation </core/schema-validation/>` |
| 45 | + in the {+server-docs-name+}. |
| 46 | + |
| 47 | +.. _laravel-eloquent-migrations: |
| 48 | + |
| 49 | +Perform Laravel Migrations |
| 50 | +-------------------------- |
| 51 | + |
| 52 | +Laravel migrations let you programmatically create, modify, and delete |
| 53 | +your database schema by running methods included in the ``Schema`` facade. |
| 54 | +The following sections explain how to author a migration class when you use |
| 55 | +a MongoDB database and how to run them. |
| 56 | + |
| 57 | +Create a Migration Class |
| 58 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 59 | + |
| 60 | +You can create migration classes manually or generate them by using the |
| 61 | +``php artisan make:migration`` command. If you generate them, you must make the |
| 62 | +following changes to perform the schema changes on your MongoDB database: |
| 63 | + |
| 64 | +- Replace the ``Illuminate\Database\Schema\Blueprint`` import with |
| 65 | + ``MongoDB\Laravel\Schema\Blueprint`` if it is referenced in your migration |
| 66 | +- Use only commands and syntax supported by {+odm-short+} |
| 67 | + |
| 68 | +.. tip:: |
| 69 | + |
| 70 | + If your default database connection is set to anything other than your |
| 71 | + MongoDB database, update the following setting to make sure the migration |
| 72 | + specifies the correct database: |
| 73 | + |
| 74 | + - Specify ``mongodb`` in the ``$connection`` field of your migration class |
| 75 | + - Set ``DB_CONNECTION=mongodb`` in your ``.env`` configuration file |
| 76 | + |
| 77 | +The following example migration class contains the following methods: |
| 78 | + |
| 79 | +- ``up()``, which creates a collection and an index when you run the migration |
| 80 | +- ``down()``, which drops the collection and all the indexes on it when you roll back the migration |
| 81 | + |
| 82 | +.. literalinclude:: /includes/schema-builder/astronauts_migration.php |
| 83 | + :dedent: |
| 84 | + :language: php |
| 85 | + :emphasize-lines: 6, 11 |
| 86 | + |
| 87 | +Run or Roll Back a Migration |
| 88 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 89 | + |
| 90 | +To run the database migration from a class file, run the following command |
| 91 | +after replacing the placeholder: |
| 92 | + |
| 93 | +.. code-block:: bash |
| 94 | + |
| 95 | + php artisan migrate --path=<path to your migration class file> |
| 96 | + |
| 97 | +This command runs the ``up()`` function in the class file to create the |
| 98 | +collection and index in the database specified in the ``config/database.php`` |
| 99 | +file. |
| 100 | + |
| 101 | +To roll back the migration, run the following command after replacing the |
| 102 | +placeholder: |
| 103 | + |
| 104 | +.. code-block:: bash |
| 105 | + |
| 106 | + php artisan migrate:rollback --path=<path to your migration class file> |
| 107 | + |
| 108 | +This command runs the ``down()`` function in the class file to drop the |
| 109 | +collection and related indexes. |
| 110 | + |
| 111 | +To learn more about Laravel migrations, see |
| 112 | +`Database: Migrations <https://laravel.com/docs/{+laravel-docs-version+}/migrations>`__ |
| 113 | +in the Laravel documentation. |
| 114 | + |
| 115 | +.. _laravel-eloquent-collection-exists: |
| 116 | + |
| 117 | +Check Whether a Collection Exists |
| 118 | +--------------------------------- |
| 119 | + |
| 120 | +To check whether a collection exists, call the ``hasCollection()`` method on |
| 121 | +the ``Schema`` facade in your migration file. You can use this to |
| 122 | +perform migration logic conditionally. |
| 123 | + |
| 124 | +The following example migration creates a ``stars`` collection if a collection |
| 125 | +named ``telescopes`` exists: |
| 126 | + |
| 127 | +.. literalinclude:: /includes/schema-builder/stars_migration.php |
| 128 | + :language: php |
| 129 | + :dedent: |
| 130 | + :start-after: begin conditional create |
| 131 | + :end-before: end conditional create |
| 132 | + |
| 133 | +.. _laravel-eloquent-indexes: |
| 134 | + |
| 135 | +Manage Indexes |
| 136 | +-------------- |
| 137 | + |
| 138 | +MongoDB indexes are data structures that improve query efficiency by reducing |
| 139 | +the number of documents needed to retrieve query results. Certain indexes, such |
| 140 | +as geospatial indexes, extend how you can query the data. |
| 141 | + |
| 142 | +To improve query performance by using an index, make sure the index covers |
| 143 | +the query. To learn more about indexes and query optimization, see the |
| 144 | +following {+server-docs-name+} entries: |
| 145 | + |
| 146 | +- :manual:`Indexes </indexes>` |
| 147 | +- :manual:`Query Optimization </core/query-optimization/>` |
| 148 | + |
| 149 | +The following sections show how you can use the schema builder to create and |
| 150 | +drop various types of indexes on a collection. |
| 151 | + |
| 152 | +Create an Index |
| 153 | +~~~~~~~~~~~~~~~ |
| 154 | + |
| 155 | +To create indexes, call the ``create()`` method on the ``Schema`` facade |
| 156 | +in your migration file. Pass it the collection name and a callback |
| 157 | +method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Specify the |
| 158 | +index creation details on the ``Blueprint`` instance. |
| 159 | + |
| 160 | +The following example migration creates indexes on the following collection |
| 161 | +fields: |
| 162 | + |
| 163 | +- Single field index on ``mission_type`` |
| 164 | +- Compound index on ``launch_location`` and ``launch_date``, specifying a descending sort order on ``launch_date`` |
| 165 | +- Unique index on the ``mission_id`` field, specifying the index name "unique_mission_id_idx" |
| 166 | + |
| 167 | +Click the :guilabel:`VIEW OUTPUT` button to see the indexes created by running |
| 168 | +the migration, including the default index on the ``_id`` field: |
| 169 | + |
| 170 | +.. io-code-block:: |
| 171 | + |
| 172 | + .. input:: /includes/schema-builder/flights_migration.php |
| 173 | + :language: php |
| 174 | + :dedent: |
| 175 | + :start-after: begin create index |
| 176 | + :end-before: end create index |
| 177 | + |
| 178 | + .. output:: |
| 179 | + :language: json |
| 180 | + :visible: false |
| 181 | + |
| 182 | + [ |
| 183 | + { v: 2, key: { _id: 1 }, name: '_id_' }, |
| 184 | + { v: 2, key: { mission_type: 1 }, name: 'mission_type_1' }, |
| 185 | + { |
| 186 | + v: 2, |
| 187 | + key: { launch_location: 1, launch_date: -1 }, |
| 188 | + name: 'launch_location_1_launch_date_-1' |
| 189 | + }, |
| 190 | + { |
| 191 | + v: 2, |
| 192 | + key: { mission_id: 1 }, |
| 193 | + name: 'unique_mission_id_idx', |
| 194 | + unique: true |
| 195 | + } |
| 196 | + ] |
| 197 | + |
| 198 | +Specify Index Options |
| 199 | +~~~~~~~~~~~~~~~~~~~~~ |
| 200 | + |
| 201 | +MongoDB index options determine how the indexes are used and stored. |
| 202 | +You can specify index options when calling an index creation method, such |
| 203 | +as ``index()``, on a ``Blueprint`` instance. |
| 204 | + |
| 205 | +The following migration code shows how to add a collation to an index as an |
| 206 | +index option. Click the :guilabel:`VIEW OUTPUT` button to see the indexes |
| 207 | +created by running the migration, including the default index on the ``_id`` |
| 208 | +field: |
| 209 | + |
| 210 | +.. io-code-block:: |
| 211 | + |
| 212 | + .. input:: /includes/schema-builder/passengers_migration.php |
| 213 | + :language: php |
| 214 | + :dedent: |
| 215 | + :start-after: begin index options |
| 216 | + :end-before: end index options |
| 217 | + |
| 218 | + .. output:: |
| 219 | + :language: json |
| 220 | + :visible: false |
| 221 | + |
| 222 | + [ |
| 223 | + { v: 2, key: { _id: 1 }, name: '_id_' }, |
| 224 | + { |
| 225 | + v: 2, |
| 226 | + key: { last_name: 1 }, |
| 227 | + name: 'passengers_collation_idx', |
| 228 | + collation: { |
| 229 | + locale: 'de@collation=phonebook', |
| 230 | + caseLevel: false, |
| 231 | + caseFirst: 'off', |
| 232 | + strength: 3, |
| 233 | + numericOrdering: true, |
| 234 | + alternate: 'non-ignorable', |
| 235 | + maxVariable: 'punct', |
| 236 | + normalization: false, |
| 237 | + backwards: false, |
| 238 | + version: '57.1' |
| 239 | + } |
| 240 | + } |
| 241 | + ] |
| 242 | + |
| 243 | +To learn more about index options, see :manual:`Options for All Index Types </reference/method/db.collection.createIndex/#options-for-all-index-types>` |
| 244 | +in the {+server-docs-name+}. |
| 245 | + |
| 246 | +Create Sparse, TTL, and Unique Indexes |
| 247 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 248 | + |
| 249 | +You can use {+odm-short+} helper methods to create the following types of |
| 250 | +indexes: |
| 251 | + |
| 252 | +- Sparse indexes, which allow index entries only for documents that contain the |
| 253 | + specified field |
| 254 | +- Time-to-live (TTL) indexes, which expire after a set amount of time |
| 255 | +- Unique indexes, which prevent inserting documents that contain duplicate |
| 256 | + values for the indexed field |
| 257 | + |
| 258 | +To create these index types, call the ``create()`` method on the ``Schema`` facade |
| 259 | +in your migration file. Pass ``create()`` the collection name and a callback |
| 260 | +method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Call the |
| 261 | +appropriate helper method on the ``Blueprint`` instance and pass the |
| 262 | +index creation details. |
| 263 | + |
| 264 | +The following migration code shows how to create a sparse and a TTL index |
| 265 | +by using the index helpers. Click the :guilabel:`VIEW OUTPUT` button to see |
| 266 | +the indexes created by running the migration, including the default index on |
| 267 | +the ``_id`` field: |
| 268 | + |
| 269 | +.. io-code-block:: |
| 270 | + |
| 271 | + .. input:: /includes/schema-builder/planets_migration.php |
| 272 | + :language: php |
| 273 | + :dedent: |
| 274 | + :start-after: begin index helpers |
| 275 | + :end-before: end index helpers |
| 276 | + |
| 277 | + .. output:: |
| 278 | + :language: json |
| 279 | + :visible: false |
| 280 | + |
| 281 | + [ |
| 282 | + { v: 2, key: { _id: 1 }, name: '_id_' }, |
| 283 | + { v: 2, key: { rings: 1 }, name: 'rings_1', sparse: true }, |
| 284 | + { |
| 285 | + v: 2, |
| 286 | + key: { last_visible_dt: 1 }, |
| 287 | + name: 'last_visible_dt_1', |
| 288 | + expireAfterSeconds: 86400 |
| 289 | + } |
| 290 | + ] |
| 291 | + |
| 292 | +You can specify sparse, TTL, and unique indexes on either a single field or |
| 293 | +compound index by specifying them in the index options. |
| 294 | + |
| 295 | +The following migration code shows how to create all three types of indexes |
| 296 | +on a single field. Click the :guilabel:`VIEW OUTPUT` button to see the indexes |
| 297 | +created by running the migration, including the default index on the ``_id`` |
| 298 | +field: |
| 299 | + |
| 300 | +.. io-code-block:: |
| 301 | + |
| 302 | + .. input:: /includes/schema-builder/planets_migration.php |
| 303 | + :language: php |
| 304 | + :dedent: |
| 305 | + :start-after: begin multi index helpers |
| 306 | + :end-before: end multi index helpers |
| 307 | + |
| 308 | + .. output:: |
| 309 | + :language: json |
| 310 | + :visible: false |
| 311 | + |
| 312 | + [ |
| 313 | + { v: 2, key: { _id: 1 }, name: '_id_' }, |
| 314 | + { |
| 315 | + v: 2, |
| 316 | + key: { last_visible_dt: 1 }, |
| 317 | + name: 'last_visible_dt_1', |
| 318 | + unique: true, |
| 319 | + sparse: true, |
| 320 | + expireAfterSeconds: 3600 |
| 321 | + } |
| 322 | + ] |
| 323 | + |
| 324 | +To learn more about these indexes, see :manual:`Index Properties </core/indexes/index-properties/>` |
| 325 | +in the {+server-docs-name+}. |
| 326 | + |
| 327 | +Create a Geospatial Index |
| 328 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 329 | + |
| 330 | +In MongoDB, geospatial indexes let you query geospatial coordinate data for |
| 331 | +inclusion, intersection, and proximity. |
| 332 | + |
| 333 | +To create geospatial indexes, call the ``create()`` method on the ``Schema`` facade |
| 334 | +in your migration file. Pass ``create()`` the collection name and a callback |
| 335 | +method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Specify the |
| 336 | +geospatial index creation details on the ``Blueprint`` instance. |
| 337 | + |
| 338 | +The following example migration creates a ``2d`` and ``2dsphere`` geospatial |
| 339 | +index on the ``spaceports`` collection. Click the :guilabel:`VIEW OUTPUT` |
| 340 | +button to see the indexes created by running the migration, including the |
| 341 | +default index on the ``_id`` field: |
| 342 | + |
| 343 | +.. io-code-block:: |
| 344 | + .. input:: /includes/schema-builder/spaceports_migration.php |
| 345 | + :language: php |
| 346 | + :dedent: |
| 347 | + :start-after: begin create geospatial index |
| 348 | + :end-before: end create geospatial index |
| 349 | + |
| 350 | + .. output:: |
| 351 | + :language: json |
| 352 | + :visible: false |
| 353 | + |
| 354 | + [ |
| 355 | + { v: 2, key: { _id: 1 }, name: '_id_' }, |
| 356 | + { |
| 357 | + v: 2, |
| 358 | + key: { launchpad_location: '2dsphere' }, |
| 359 | + name: 'launchpad_location_2dsphere', |
| 360 | + '2dsphereIndexVersion': 3 |
| 361 | + }, |
| 362 | + { v: 2, key: { runway_location: '2d' }, name: 'runway_location_2d' } |
| 363 | + ] |
| 364 | + |
| 365 | + |
| 366 | +To learn more about geospatial indexes, see |
| 367 | +:manual:`Geospatial Indexes </core/indexes/index-types/index-geospatial/>` in |
| 368 | +the {+server-docs-name+}. |
| 369 | + |
| 370 | +Drop an Index |
| 371 | +~~~~~~~~~~~~~ |
| 372 | + |
| 373 | +To drop indexes from a collection, call the ``table()`` method on the |
| 374 | +``Schema`` facade in your migration file. Pass it the table name and a |
| 375 | +callback method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. |
| 376 | +Call the ``dropIndex()`` method with the index name on the ``Blueprint`` |
| 377 | +instance. |
| 378 | + |
| 379 | +.. note:: |
| 380 | + |
| 381 | + If you drop a collection, MongoDB automatically drops all the indexes |
| 382 | + associated with it. |
| 383 | + |
| 384 | +The following example migration drops an index called ``unique_mission_id_idx`` |
| 385 | +from the ``flights`` collection: |
| 386 | + |
| 387 | +.. literalinclude:: /includes/schema-builder/flights_migration.php |
| 388 | + :language: php |
| 389 | + :dedent: |
| 390 | + :start-after: begin drop index |
| 391 | + :end-before: end drop index |
| 392 | + |
| 393 | + |
0 commit comments