@@ -352,8 +352,9 @@ collection that contains data with different schemas.
352
352
353
353
To use this feature with models that use MongoDB as a database, add the
354
354
``MongoDB\Laravel\Eloquent\HasSchemaVersion`` import to your model.
355
- Then, set the ``SCHEMA_VERSION`` constant to set the current schema
356
- version for your collection.
355
+ Then, set the ``SCHEMA_VERSION`` constant to ``1`` to set the first
356
+ schema version on your collection. If your collection evolves to contain
357
+ multiple schemas, you can change the value of the ``SCHEMA_VERSION`` constant.
357
358
358
359
When creating your model, you can define the ``migrateSchema()`` method
359
360
to specify a migration to the current schema version upon saving a
@@ -373,33 +374,28 @@ The example class is defined with the following behavior:
373
374
374
375
.. literalinclude:: /includes/eloquent-models/PlanetSchemaVersion.php
375
376
:language: php
376
- :emphasize-lines: 10,12,14
377
+ :emphasize-lines: 10,12,16
377
378
:dedent:
378
379
379
- When you save a model in which the ``schema_version`` field is
380
- absent or the value is less than ``2``, Laravel adds the ``galaxy``
380
+ When you save a model that does not have a schema version specified,
381
+ Laravel uses the latest schema version, but does not perform the
382
+ migration.
383
+
384
+ When you save a model in which the ``schema_version`` field value is
385
+ less than ``2``, Laravel adds the ``galaxy``
381
386
field and updates the schema version to the current version (``2``).
382
387
383
- The following code inserts instances of the ``Planet`` model that are
384
- not at the current schema version, then retrieves the models from the
385
- collection to demonstrate the migration changes:
388
+ The following code inserts two instances of the ``Planet`` model, then
389
+ retrieves the models from the collection to demonstrate the changes:
386
390
387
391
.. io-code-block::
388
392
:copyable: true
389
393
390
- .. input::
394
+ .. input:: /includes/eloquent-models/SchemaVersionTest.php
391
395
:language: php
392
-
393
- $saturn = Planet::create([
394
- 'name' => 'Saturn',
395
- 'type' => 'gas',
396
- ]);
397
-
398
- $wasp = Planet::create([
399
- 'name' => 'WASP-39 b',
400
- 'type' => 'gas',
401
- 'schema_version' => 1,
402
- ]);
396
+ :dedent:
397
+ :start-after: begin-schema-version
398
+ :end-before: end-schema-version
403
399
404
400
$planets = Planet::where('type', 'gas')
405
401
->get();
0 commit comments