Skip to content

Commit 768541c

Browse files
committed
Merge remote-tracking branch 'upstream/5.0' into DOCSP-41335-id-alias
2 parents 06e4c46 + 7296a25 commit 768541c

File tree

111 files changed

+3091
-1400
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+3091
-1400
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
* @mongodb/dbx-php
2-
/docs @mongodb/docs-drivers-team
2+
/docs @mongodb/laravel-docs

CHANGELOG.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4-
## [4.9.0] - coming soon
4+
## [5.0.0] - next
55

6+
* **BREAKING CHANGE** Use `id` as an alias for `_id` in commands and queries for compatibility with Eloquent packages by @GromNaN in [#3040](https://github.com/mongodb/laravel-mongodb/pull/3040)
7+
* **BREAKING CHANGE** Make Query\Builder return objects instead of array to match Laravel behavior by @GromNaN in [#3107](https://github.com/mongodb/laravel-mongodb/pull/3107)
8+
9+
## [4.8.0] - 2024-08-27
10+
11+
* Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550)
12+
* Add `Query\Builder::whereLike()` and `whereNotLike()` methods by @GromNaN in [#3108](https://github.com/mongodb/laravel-mongodb/pull/3108)
13+
* Deprecate `Connection::collection()` and `Schema\Builder::collection()` methods by @GromNaN in [#3062](https://github.com/mongodb/laravel-mongodb/pull/3062)
14+
* Deprecate `Model::$collection` property to customize collection name. Use `$table` instead by @GromNaN in [#3064](https://github.com/mongodb/laravel-mongodb/pull/3064)
15+
16+
## [4.7.2] - 2024-08-27
17+
18+
* Add `Query\Builder::upsert()` method with a single document by @GromNaN in [#3100](https://github.com/mongodb/laravel-mongodb/pull/3100)
19+
20+
## [4.7.1] - 2024-07-25
21+
22+
* Fix registration of `BusServiceProvider` for compatibility with Horizon by @GromNaN in [#3071](https://github.com/mongodb/laravel-mongodb/pull/3071)
23+
24+
## [4.7.0] - 2024-07-19
25+
26+
* Add `Query\Builder::upsert()` method by @GromNaN in [#3052](https://github.com/mongodb/laravel-mongodb/pull/3052)
627
* Add `Connection::getServerVersion()` by @GromNaN in [#3043](https://github.com/mongodb/laravel-mongodb/pull/3043)
7-
* Add `Schema\Builder::getTables()` and `getTableListing` by @GromNaN in [#3044](https://github.com/mongodb/laravel-mongodb/pull/3044)
28+
* Add `Schema\Builder::getTables()` and `getTableListing()` by @GromNaN in [#3044](https://github.com/mongodb/laravel-mongodb/pull/3044)
29+
* Add `Schema\Builder::getColumns()` and `getIndexes()` by @GromNaN in [#3045](https://github.com/mongodb/laravel-mongodb/pull/3045)
30+
* Add `Schema\Builder::hasColumn()` and `hasColumns()` method by @Alex-Belyi in [#3001](https://github.com/mongodb/laravel-mongodb/pull/3001)
31+
* Fix unsetting a field in an embedded model by @GromNaN in [#3052](https://github.com/mongodb/laravel-mongodb/pull/3052)
832

933
## [4.6.0] - 2024-07-09
1034

docs/cache.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ adds 5, and removes 2.
150150

151151
.. note::
152152

153-
{+odm-short+} supports incrementing and decrementing with integer and float values.
153+
The {+odm-short+} supports incrementing and decrementing with integer and float values.
154154

155155
For more information about using the cache, see the `Laravel Cache documentation
156156
<https://laravel.com/docs/{+laravel-docs-version+}/cache#cache-usage>`__.

docs/compatibility.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ Laravel Compatibility
2121
---------------------
2222

2323
The following compatibility table specifies the versions of Laravel and
24-
{+odm-short+} that you can use together.
24+
the {+odm-short+} that you can use together.
2525

2626
.. include:: /includes/framework-compatibility-laravel.rst
2727

28-
To find compatibility information for unmaintained versions of {+odm-short+},
28+
To find compatibility information for unmaintained versions of the {+odm-short+},
2929
see `Laravel Version Compatibility <{+mongodb-laravel-gh+}/blob/3.9/README.md#installation>`__
3030
on GitHub.

docs/eloquent-models.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ Eloquent Models
1313

1414
Eloquent models are part of the Laravel Eloquent object-relational
1515
mapping (ORM) framework, which lets you to work with data in a relational
16-
database by using model classes and Eloquent syntax. {+odm-short+} extends
16+
database by using model classes and Eloquent syntax. The {+odm-short+} extends
1717
this framework so that you can use Eloquent syntax to work with data in a
1818
MongoDB database.
1919

2020
This section contains guidance on how to use Eloquent models in
21-
{+odm-short+} to work with MongoDB in the following ways:
21+
the {+odm-short+} to work with MongoDB in the following ways:
2222

2323
- :ref:`laravel-eloquent-model-class` shows how to define models and customize
2424
their behavior

docs/eloquent-models/model-class.txt

Lines changed: 154 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@ Eloquent Model Class
2020
Overview
2121
--------
2222

23-
This guide shows you how to use the {+odm-long+} to define and
23+
This guide shows you how to use {+odm-long+} to define and
2424
customize Laravel Eloquent models. You can use these models to work with
2525
MongoDB data by using the Laravel Eloquent object-relational mapper (ORM).
2626

2727
The following sections explain how to add Laravel Eloquent ORM behaviors
28-
to {+odm-short+} models:
28+
to {+odm-long+} models:
2929

3030
- :ref:`laravel-model-define` demonstrates how to create a model class.
3131
- :ref:`laravel-authenticatable-model` shows how to set MongoDB as the
3232
authentication user provider.
33-
- :ref:`laravel-model-customize` explains several model class customizations.
33+
- :ref:`laravel-model-customize` explains several model class
34+
customizations.
35+
- :ref:`laravel-third-party-model` explains how to make third-party
36+
model classes compatible with MongoDB.
3437
- :ref:`laravel-model-pruning` shows how to periodically remove models that
3538
you no longer need.
39+
- :ref:`laravel-schema-versioning` shows how to implement model schema
40+
versioning.
3641

3742
.. _laravel-model-define:
3843

@@ -42,7 +47,7 @@ Define an Eloquent Model Class
4247
Eloquent models are classes that represent your data. They include methods
4348
that perform database operations such as inserts, updates, and deletes.
4449

45-
To declare a {+odm-short+} model, create a class in the ``app/Models``
50+
To declare a {+odm-long+} model, create a class in the ``app/Models``
4651
directory of your Laravel application that extends
4752
``MongoDB\Laravel\Eloquent\Model`` as shown in the following code example:
4853

@@ -67,15 +72,14 @@ This model is stored in the ``planets`` MongoDB collection.
6772
To learn how to specify the database name that your Laravel application uses,
6873
:ref:`laravel-quick-start-connect-to-mongodb`.
6974

70-
7175
.. _laravel-authenticatable-model:
7276

7377
Extend the Authenticatable Model
7478
--------------------------------
7579

7680
To configure MongoDB as the Laravel user provider, you can extend the
77-
{+odm-short+} ``MongoDB\Laravel\Auth\User`` class. The following code example
78-
shows how to extend this class:
81+
{+odm-short+} ``MongoDB\Laravel\Auth\User`` class. The following code
82+
example shows how to extend this class:
7983

8084
.. literalinclude:: /includes/eloquent-models/AuthenticatableUser.php
8185
:language: php
@@ -107,7 +111,7 @@ Change the Model Collection Name
107111

108112
By default, the model uses the snake case plural form of your model
109113
class name. To change the name of the collection the model uses to retrieve
110-
and save data in MongoDB, override the ``$collection`` property of the model
114+
and save data in MongoDB, override the ``$table`` property of the model
111115
class.
112116

113117
.. note::
@@ -123,7 +127,7 @@ The following example specifies the custom MongoDB collection name,
123127
:emphasize-lines: 9
124128
:dedent:
125129

126-
Without overriding the ``$collection`` property, this model maps to the
130+
Without overriding the ``$table`` property, this model maps to the
127131
``planets`` collection. With the overridden property, the example class stores
128132
the model in the ``celestial_body`` collection.
129133

@@ -151,7 +155,7 @@ To learn more about primary key behavior and customization options, see
151155
in the Laravel docs.
152156

153157
To learn more about the ``_id`` field, ObjectIDs, and the MongoDB document
154-
structure, see :manual:`Documents </core/document>` in the MongoDB server docs.
158+
structure, see :manual:`Documents </core/document>` in the Server manual.
155159

156160
.. _laravel-model-soft-delete:
157161

@@ -179,7 +183,7 @@ in the Laravel docs.
179183
.. _laravel-model-cast-data-types:
180184

181185
Cast Data Types
182-
---------------
186+
~~~~~~~~~~~~~~~
183187

184188
Eloquent lets you convert model attribute data types before storing or
185189
retrieving data by using a casting helper. This helper is a convenient
@@ -223,7 +227,7 @@ less than three years ago:
223227
Planet::where( 'discovery_dt', '>', new DateTime('-3 years'))->get();
224228

225229
To learn more about MongoDB's data types, see :manual:`BSON Types </reference/bson-types/>`
226-
in the MongoDB server docs.
230+
in the Server manual.
227231

228232
To learn more about the Laravel casting helper and supported types, see `Attribute Casting <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-mutators#attribute-casting>`__
229233
in the Laravel docs.
@@ -280,6 +284,45 @@ To learn how to change the behavior when attempting to fill a field omitted
280284
from the ``$fillable`` array, see `Mass Assignment Exceptions <https://laravel.com/docs/{+laravel-docs-version+}/eloquent#mass-assignment-exceptions>`__
281285
in the Laravel docs.
282286

287+
.. _laravel-third-party-model:
288+
289+
Extend Third-Party Model Classes
290+
--------------------------------
291+
292+
You can use the {+odm-short+} to extend a third-party model class by
293+
including the ``DocumentModel`` trait when defining your model class. By
294+
including this trait, you can make the third-party class compatible with
295+
MongoDB.
296+
297+
When you apply the ``DocumentModel`` trait to a model class, you must
298+
declare the following properties in your class:
299+
300+
- ``$primaryKey = '_id'``, because the ``_id`` field uniquely
301+
identifies MongoDB documents
302+
- ``$keyType = 'string'``, because the {+odm-short+} casts MongoDB
303+
``ObjectId`` values to type ``string``
304+
305+
Extended Class Example
306+
~~~~~~~~~~~~~~~~~~~~~~
307+
308+
This example creates a ``Planet`` model class that extends the
309+
``CelestialBody`` class from a package called ``ThirdPartyPackage``. The
310+
``Post`` class includes the ``DocumentModel`` trait and defines
311+
properties including ``$primaryKey`` and ``$keyType``:
312+
313+
.. literalinclude:: /includes/eloquent-models/PlanetThirdParty.php
314+
:language: php
315+
:emphasize-lines: 10,13-14
316+
:dedent:
317+
318+
After defining your class, you can perform MongoDB operations as usual.
319+
320+
.. tip::
321+
322+
To view another example that uses the ``DocumentModel`` trait, see
323+
the :ref:`laravel-user-auth-sanctum` section of the User
324+
Authentication guide.
325+
283326
.. _laravel-model-pruning:
284327

285328
Specify Pruning Behavior
@@ -301,7 +344,7 @@ appropriate import to your model:
301344
.. note::
302345

303346
When enabling soft deletes on a mass prunable model, you must import the
304-
following {+odm-short+} packages:
347+
following {+odm-long+} packages:
305348

306349
- ``MongoDB\Laravel\Eloquent\SoftDeletes``
307350
- ``MongoDB\Laravel\Eloquent\MassPrunable``
@@ -333,3 +376,101 @@ models that the prune action deletes:
333376
:emphasize-lines: 5,10,12
334377
:dedent:
335378

379+
.. _laravel-schema-versioning:
380+
381+
Create a Versioned Model Schema
382+
-------------------------------
383+
384+
You can implement a schema versioning pattern into your application by
385+
using the ``HasSchemaVersion`` trait on an Eloquent model. You might
386+
choose to implement a schema version to organize or standardize a
387+
collection that contains data with different schemas.
388+
389+
.. tip::
390+
391+
To learn more about schema versioning, see the :manual:`Model Data for
392+
Schema Versioning </tutorial/model-data-for-schema-versioning/>`
393+
tutorial in the {+server-docs-name+}.
394+
395+
To use this feature with models that use MongoDB as a database, add the
396+
``MongoDB\Laravel\Eloquent\HasSchemaVersion`` import to your model.
397+
Then, set the ``SCHEMA_VERSION`` constant to ``1`` to set the first
398+
schema version on your collection. If your collection evolves to contain
399+
multiple schemas, you can update the value of the ``SCHEMA_VERSION``
400+
constant in subsequent model classes.
401+
402+
When creating your model, you can define the ``migrateSchema()`` method
403+
to specify a migration to the current schema version upon retrieving a
404+
model. In this method, you can specify the changes to make to an older
405+
model to update it to match the current schema version.
406+
407+
When you save a model that does not have a schema version
408+
specified, the ``HasSchemaVersion`` trait assumes that it follows the
409+
latest schema version. When you retrieve a model that does not contain
410+
the ``schema_version`` field, the trait assumes that its schema version
411+
is ``0`` and performs the migration.
412+
413+
Schema Versioning Example
414+
~~~~~~~~~~~~~~~~~~~~~~~~~
415+
416+
In this sample situation, you are working with a collection that was
417+
first modeled by the following class:
418+
419+
.. literalinclude:: /includes/eloquent-models/PlanetSchemaVersion1.php
420+
:language: php
421+
:dedent:
422+
423+
Now, you want to implement a new schema version on the collection.
424+
You can define the new model class with the following behavior:
425+
426+
- Implements the ``HasSchemaVersion`` trait and sets the current
427+
``SCHEMA_VERSION`` to ``2``
428+
429+
- Defines the ``migrateSchema()`` method to migrate models in which the
430+
schema version is less than ``2`` to have a ``galaxy`` field that has a value
431+
of ``'Milky Way'``
432+
433+
.. literalinclude:: /includes/eloquent-models/PlanetSchemaVersion2.php
434+
:language: php
435+
:emphasize-lines: 10,12,20
436+
:dedent:
437+
438+
In the ``"WASP-39 b"`` document in the following code, the
439+
``schema_version`` field value is less than ``2``. When you retrieve the
440+
document, the {+odm-short+} adds the ``galaxy`` field and updates the schema
441+
version to the current version, ``2``.
442+
443+
The ``"Saturn"`` document does not contain the ``schema_version`` field,
444+
so the {+odm-short+} assigns it the current schema version upon saving.
445+
446+
Finally, the code retrieves the models from the collection to
447+
demonstrate the changes:
448+
449+
.. io-code-block::
450+
:copyable: true
451+
452+
.. input:: /includes/eloquent-models/SchemaVersionTest.php
453+
:language: php
454+
:dedent:
455+
:start-after: begin-schema-version
456+
:end-before: end-schema-version
457+
458+
.. output::
459+
:language: none
460+
:visible: false
461+
462+
[
463+
{
464+
"_id": ...,
465+
"name": "WASP-39 b",
466+
"type": "gas",
467+
"galaxy": "Milky Way",
468+
"schema_version": 2,
469+
},
470+
{
471+
"_id": ...,
472+
"name": "Saturn",
473+
"type": "gas",
474+
"schema_version": 2,
475+
}
476+
]

0 commit comments

Comments
 (0)