@@ -21,8 +21,9 @@ Overview
21
21
--------
22
22
23
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.
24
+ which lets you create and modify tables, or collections in MongoDB.
25
+ Facades are static interfaces to classes that make the syntax more
26
+ concise and improve testability.
26
27
27
28
The {+odm-short+} supports a subset of the index and collection management methods
28
29
in the Laravel ``Schema`` facade.
@@ -33,16 +34,10 @@ in the Laravel documentation.
33
34
The following sections describe the Laravel schema builder features available
34
35
in the {+odm-short+} and show examples of how to use them:
35
36
36
- - :ref:`<laravel-eloquent-migrations>`
37
- - :ref:`<laravel-eloquent-collection-exists>`
38
- - :ref:`<laravel-eloquent-indexes>`
39
-
40
- .. note::
41
-
42
- The {+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+}.
37
+ - :ref:`laravel-eloquent-migrations`
38
+ - :ref:`laravel-eloquent-schema-validation`
39
+ - :ref:`laravel-eloquent-collection-exists`
40
+ - :ref:`laravel-eloquent-indexes`
46
41
47
42
.. _laravel-eloquent-migrations:
48
43
@@ -117,6 +112,60 @@ To learn more about Laravel migrations, see
117
112
`Database: Migrations <https://laravel.com/docs/{+laravel-docs-version+}/migrations>`__
118
113
in the Laravel documentation.
119
114
115
+ .. _laravel-eloquent-schema-validation:
116
+
117
+ Implement Schema Validation
118
+ ---------------------------
119
+
120
+ You can use the ``jsonSchema()`` method to implement :manual:`schema
121
+ validation </core/schema-validation/>` when using the following schema
122
+ builder methods:
123
+
124
+ - ``Schema::create()``: When creating a new collection
125
+ - ``Schema::table()``: When updating collection properties
126
+
127
+ You can use schema validation to restrict data types and value ranges of
128
+ document fields in a specified collection. After you implement schema
129
+ validation, the server restricts write operations that don't follow the
130
+ validation rules.
131
+
132
+ You can pass the following parameters to ``jsonSchema()``:
133
+
134
+ - ``schema``: Array that specifies the validation rules for the
135
+ collection. To learn more about constructing a schema, see
136
+ the :manual:`$jsonSchema </reference/operator/query/jsonSchema/>`
137
+ reference in the {+server-docs-name+}.
138
+
139
+ - ``validationLevel``: Sets the level of validation enforcement.
140
+ Accepted values are ``"strict"`` (default) and ``"moderate"``.
141
+
142
+ - ``validationAction``: Specifies the action to take when invalid
143
+ operations are attempted. Accepted values are ``"error"`` (default) and
144
+ ``"warn"``.
145
+
146
+ This example demonstrates how to specify a schema in the
147
+ ``jsonSchema()`` method when creating a collection. The schema
148
+ validation has the following specifications:
149
+
150
+ - Documents in the ``pilots`` collection must
151
+ contain the ``license_number`` field.
152
+
153
+ - The ``license_number`` field must have an integer value between
154
+ ``1000`` and ``9999``.
155
+
156
+ - If you attempt to perform invalid write operations, the server raises
157
+ an error.
158
+
159
+ .. literalinclude:: /includes/schema-builder/flights_migration.php
160
+ :language: php
161
+ :dedent:
162
+ :start-after: begin-json-schema
163
+ :end-before: end-json-schema
164
+
165
+ If you attempt to insert a document into the ``pilots`` collection that
166
+ violates the schema validation rule, {+odm-long+} returns a
167
+ :php:`BulkWriteException <mongodb-driver-exception-bulkwriteexception>`.
168
+
120
169
.. _laravel-eloquent-collection-exists:
121
170
122
171
Check Whether a Collection Exists
0 commit comments