Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3c0b011

Browse files
Merge 4.8 into 5.0 (#3069)
2 parents d370221 + d52f24c commit 3c0b011

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

‎docs/eloquent-models/model-class.txt

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ to {+odm-short+} models:
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.
3639
- :ref:`laravel-schema-versioning` shows how to implement model schema
@@ -180,7 +183,7 @@ in the Laravel docs.
180183
.. _laravel-model-cast-data-types:
181184

182185
Cast Data Types
183-
---------------
186+
~~~~~~~~~~~~~~~
184187

185188
Eloquent lets you convert model attribute data types before storing or
186189
retrieving data by using a casting helper. This helper is a convenient
@@ -281,6 +284,45 @@ To learn how to change the behavior when attempting to fill a field omitted
281284
from the ``$fillable`` array, see `Mass Assignment Exceptions <https://laravel.com/docs/{+laravel-docs-version+}/eloquent#mass-assignment-exceptions>`__
282285
in the Laravel docs.
283286

287+
.. _laravel-third-party-model:
288+
289+
Extend Third-Party Model Classes
290+
--------------------------------
291+
292+
You can use {+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 {+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+
284326
.. _laravel-model-pruning:
285327

286328
Specify Pruning Behavior
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use MongoDB\Laravel\Eloquent\DocumentModel;
6+
use ThirdPartyPackage\CelestialBody;
7+
8+
class Planet extends CelestialBody
9+
{
10+
use DocumentModel;
11+
12+
protected $fillable = ['name', 'diameter'];
13+
protected $primaryKey = '_id';
14+
protected $keyType = 'string';
15+
}

‎docs/user-authentication.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ service providers. To learn more, see `Overriding Default Models
149149
<https://laravel.com/docs/{+laravel-docs-version+}/sanctum#overriding-default-models>`__
150150
in the Laravel Sanctum guide.
151151

152+
.. tip::
153+
154+
To learn more about the ``DocumentModel`` trait, see
155+
:ref:`laravel-third-party-model` in the Eloquent Model Class guide.
156+
152157
.. _laravel-user-auth-reminders:
153158

154159
Password Reminders

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /