1
- .. _laravel-eloquent- schema-builder:
1
+ .. _laravel-schema-builder:
2
2
3
3
==============
4
4
Schema Builder
@@ -14,14 +14,14 @@ Schema Builder
14
14
Overview
15
15
--------
16
16
17
- Laravel provides a ``Schema`` **facade** to access the schema builder, which
18
- lets you create and modify tables. Facades are static interfaces to classes
19
- that make the syntax more concise and improve testability.
17
+ Laravel provides a **facade** to access the schema builder class ``Schema``,
18
+ which lets you create and modify tables. Facades are static interfaces to
19
+ classes that make the syntax more concise and improve testability.
20
20
21
- {+odm-short+} supports index and collection management methods of the
22
- Laravel ``Schema`` facade.
21
+ {+odm-short+} supports a subset of the index and collection management methods
22
+ in the Laravel ``Schema`` facade.
23
23
24
- To learn more about Laravel Facades , see `Facades <https://laravel.com/docs/{+laravel-docs-version+}/facades>`__
24
+ To learn more about facades , see `Facades <https://laravel.com/docs/{+laravel-docs-version+}/facades>`__
25
25
in the Laravel documentation.
26
26
27
27
The following sections describe the Laravel schema builder and functionality
@@ -37,15 +37,19 @@ available in {+odm-short+} and show examples of how to use them:
37
37
Perform Laravel Migrations
38
38
--------------------------
39
39
40
- Laravel lets you create and make modifications to your database schema by
41
- running methods that call the ``Schema`` facade. The process of running this
42
- logic is called a migration.
40
+ Laravel migrations let you programmatically create, modify, and delete
41
+ your database schema by running methods included in the ``Schema`` facade.
42
+ The following sections explain how to author a migration class when you use
43
+ a MongoDB database and how to run them.
43
44
44
- You can generate the migration class files by using the ``php artisan make:migration``
45
- command. Make the following changes to the generated files to perform the
46
- migration on your MongoDB database:
45
+ Create a Migration Class
46
+ ~~~~~~~~~~~~~~~~~~~~~~~~
47
47
48
- - Replace the ``Illuminate\Database\Schema\Blueprint`` import with
48
+ You can create migration classes manually or generate them by using the
49
+ ``php artisan make:migration`` command. If you generate them, you must make the
50
+ following changes to perform the schema changes on your MongoDB database:
51
+
52
+ - Replace the ``Illuminate\Database\Schema\Blueprint`` import with
49
53
``MongoDB\Laravel\Schema\Blueprint``
50
54
- Specify ``mongodb`` in the ``$connection`` field
51
55
- Use only commands and syntax supported by {+odm-short+}
@@ -55,14 +59,23 @@ migration on your MongoDB database:
55
59
Make sure you set ``DB_CONNECTION=mongodb`` in your ``.env`` configuration
56
60
file so that PHP artisan performs the migration on the correct database.
57
61
62
+ The following example migration class file that creates a collection and
63
+ index when you run the migration and drops it when you roll back the migration:
64
+
65
+ .. literalinclude:: /includes/schema-builder/astronauts_migration.php
66
+ :language: php
67
+
68
+ Run or Roll Back a Migration
69
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70
+
58
71
To run the database migration from a class file, run the following PHP artisan
59
72
command:
60
73
61
74
.. code-block:: bash
62
75
63
76
php artisan migrate --path=<path to your migration class file>
64
77
65
- This artisan command runs the ``up()`` method in the class file to create
78
+ This artisan command runs the ``up()`` function in the class file to create
66
79
the collection and index in the database specified in the ``config/database.php``
67
80
file.
68
81
@@ -72,14 +85,12 @@ To roll back the migration, run the following PHP artisan command:
72
85
73
86
php artisan migrate:rollback --path=<path to your migration class file>
74
87
75
- This command runs the ``down()`` method in the class file to drop the
88
+ This command runs the ``down()`` function in the class file to drop the
76
89
collection and related indexes.
77
90
78
- The following example migration class file that creates a collection and
79
- index when you run the migration and drops it when you roll back the migration:
80
-
81
- .. literalinclude:: /includes/schema-builder/astronauts_migration.php
82
- :language: php
91
+ To learn more about Laravel migrations, see
92
+ `Database: Migrations <https://laravel.com/docs/{+laravel-docs-version+}/migrations>`__
93
+ in the Laravel documentation.
83
94
84
95
.. _laravel-eloquent-indexes:
85
96
@@ -95,6 +106,9 @@ Manage Collections
95
106
96
107
TODO
97
108
109
+ Note that creating a collection is a noop unless it also creates an index
110
+ since MongoDB automatically creates collections when you first write to them.
111
+
98
112
.. _laravel-eloquent-customize-collection:
99
113
100
114
Customize Collection Creation
0 commit comments