From b0c3a95f2a7ad14e5b8de2ca9b0bee4f16c112a1 Mon Sep 17 00:00:00 2001 From: Rea Rustagi <85902999+rustagir@users.noreply.github.com> Date: Tue, 23 Jul 2024 10:40:31 -0400 Subject: [PATCH] Messed up the base branch/merge - fix (#3065) --- docs/eloquent-models/model-class.txt | 46 ++++++++++++++++++- .../eloquent-models/PlanetThirdParty.php | 15 ++++++ docs/user-authentication.txt | 5 ++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 docs/includes/eloquent-models/PlanetThirdParty.php diff --git a/docs/eloquent-models/model-class.txt b/docs/eloquent-models/model-class.txt index ad5565abe..9d38fe1a7 100644 --- a/docs/eloquent-models/model-class.txt +++ b/docs/eloquent-models/model-class.txt @@ -30,7 +30,10 @@ to {+odm-short+} models: - :ref:`laravel-model-define` demonstrates how to create a model class. - :ref:`laravel-authenticatable-model` shows how to set MongoDB as the authentication user provider. -- :ref:`laravel-model-customize` explains several model class customizations. +- :ref:`laravel-model-customize` explains several model class + customizations. +- :ref:`laravel-third-party-model` explains how to make third-party + model classes compatible with MongoDB. - :ref:`laravel-model-pruning` shows how to periodically remove models that you no longer need. - :ref:`laravel-schema-versioning` shows how to implement model schema @@ -180,7 +183,7 @@ in the Laravel docs. .. _laravel-model-cast-data-types: Cast Data Types ---------------- +~~~~~~~~~~~~~~~ Eloquent lets you convert model attribute data types before storing or 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 from the ``$fillable`` array, see `Mass Assignment Exceptions `__ in the Laravel docs. +.. _laravel-third-party-model: + +Extend Third-Party Model Classes +-------------------------------- + +You can use {+odm-short+} to extend a third-party model class by +including the ``DocumentModel`` trait when defining your model class. By +including this trait, you can make the third-party class compatible with +MongoDB. + +When you apply the ``DocumentModel`` trait to a model class, you must +declare the following properties in your class: + +- ``$primaryKey = '_id'``, because the ``_id`` field uniquely + identifies MongoDB documents +- ``$keyType = 'string'``, because {+odm-short+} casts MongoDB + ``ObjectId`` values to type ``string`` + +Extended Class Example +~~~~~~~~~~~~~~~~~~~~~~ + +This example creates a ``Planet`` model class that extends the +``CelestialBody`` class from a package called ``ThirdPartyPackage``. The +``Post`` class includes the ``DocumentModel`` trait and defines +properties including ``$primaryKey`` and ``$keyType``: + +.. literalinclude:: /includes/eloquent-models/PlanetThirdParty.php + :language: php + :emphasize-lines: 10,13-14 + :dedent: + +After defining your class, you can perform MongoDB operations as usual. + +.. tip:: + + To view another example that uses the ``DocumentModel`` trait, see + the :ref:`laravel-user-auth-sanctum` section of the User + Authentication guide. + .. _laravel-model-pruning: Specify Pruning Behavior diff --git a/docs/includes/eloquent-models/PlanetThirdParty.php b/docs/includes/eloquent-models/PlanetThirdParty.php new file mode 100644 index 000000000..0f3bae638 --- /dev/null +++ b/docs/includes/eloquent-models/PlanetThirdParty.php @@ -0,0 +1,15 @@ +`__ in the Laravel Sanctum guide. +.. tip:: + + To learn more about the ``DocumentModel`` trait, see + :ref:`laravel-third-party-model` in the Eloquent Model Class guide. + .. _laravel-user-auth-reminders: Password Reminders