Skip to content

Commit e114452

Browse files
committed
JM tech review 1
1 parent 16aff95 commit e114452

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

docs/eloquent-models/model-class.txt

+16-20
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,9 @@ collection that contains data with different schemas.
352352

353353
To use this feature with models that use MongoDB as a database, add the
354354
``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.
357358

358359
When creating your model, you can define the ``migrateSchema()`` method
359360
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:
373374

374375
.. literalinclude:: /includes/eloquent-models/PlanetSchemaVersion.php
375376
:language: php
376-
:emphasize-lines: 10,12,14
377+
:emphasize-lines: 10,12,16
377378
:dedent:
378379

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``
381386
field and updates the schema version to the current version (``2``).
382387

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:
386390

387391
.. io-code-block::
388392
:copyable: true
389393

390-
.. input::
394+
.. input:: /includes/eloquent-models/SchemaVersionTest.php
391395
: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
403399

404400
$planets = Planet::where('type', 'gas')
405401
->get();

docs/includes/eloquent-models/PlanetSchemaVersion.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Planet extends Model
1111

1212
public const SCHEMA_VERSION = 2;
1313

14+
// Migrate documents with a lower schema version to the most current
15+
// schema when inserting new data or retrieving from the database
1416
public function migrateSchema(int $fromVersion): void
1517
{
1618
if ($fromVersion < 2) {

docs/includes/eloquent-models/SchemaVersionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function testSchemaVersion(): void
1919

2020
Planet::truncate();
2121

22+
// begin-schema-version
2223
$saturn = Planet::create([
2324
'name' => 'Saturn',
2425
'type' => 'gas',
@@ -32,8 +33,7 @@ public function testSchemaVersion(): void
3233

3334
$planets = Planet::where('type', 'gas')
3435
->get();
35-
36-
echo 'After migration:\n' . $planets;
36+
// end-schema-version
3737

3838
$this->assertCount(2, $planets);
3939

0 commit comments

Comments
 (0)