|
| 1 | +.. _laravel-fundamentals-write-ops: |
| 2 | + |
| 3 | +================ |
| 4 | +Write Operations |
| 5 | +================ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: tutorial |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: insert, insert one, update, update one, upsert, delete, delete many, code example, mass assignment, eloquent model |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use {+odm-short+} to perform |
| 24 | +**write operations** on your MongoDB collections. Write operations include |
| 25 | +inserting, updating, and deleting data based on specified criteria. |
| 26 | + |
| 27 | +This guide shows you how to perform the following tasks: |
| 28 | + |
| 29 | +- :ref:`laravel-fundamentals-insert-documents` |
| 30 | +- Modify Documents |
| 31 | +- Delete Documents |
| 32 | + |
| 33 | +.. _laravel-fundamentals-insert-documents: |
| 34 | + |
| 35 | +Insert Documents |
| 36 | +---------------- |
| 37 | + |
| 38 | +In this section, you can learn how to insert documents into MongoDB collections |
| 39 | +from your Laravel application by using the {+odm-long+}. |
| 40 | + |
| 41 | +When you insert the documents, ensure the data does not violate any |
| 42 | +unique indexes on the collection. When inserting the first document of a |
| 43 | +collection or creating a new collection, MongoDB automatically creates a |
| 44 | +unique index on the ``_id`` field. |
| 45 | + |
| 46 | +For more information on creating indexes on MongoDB collections by using the |
| 47 | +Laravel schema builder, see the :ref:`laravel-eloquent-indexes` section |
| 48 | +of the Schema Builder documentation. |
| 49 | + |
| 50 | +This section uses the following example model class to demonstrate how to |
| 51 | +use Eloquent models to perform insert operations: |
| 52 | + |
| 53 | +.. literalinclude:: /includes/fundamentals/write-operations/Concert.php |
| 54 | + :language: php |
| 55 | + :dedent: |
| 56 | + :caption: Concert.php |
| 57 | + |
| 58 | +.. tip:: |
| 59 | + |
| 60 | + The ``$fillable`` attribute lets you use Laravel mass assignment for insert |
| 61 | + operations. To learn more about mass assignment, see :ref:`laravel-model-mass-assignment` |
| 62 | + in the Eloquent Model Class documentation. |
| 63 | + |
| 64 | + The ``$casts`` attribute instructs Laravel to convert attributes to common |
| 65 | + data types. To learn more, see `Attribute Casting <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-mutators#attribute-casting>`__ |
| 66 | + in the Laravel documentation. |
| 67 | + |
| 68 | +To learn more about Eloquent models in {+odm-short+}, see the :ref:`laravel-eloquent-models` |
| 69 | +section. |
| 70 | + |
| 71 | +Insert a Document Examples |
| 72 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 73 | + |
| 74 | +These examples show how to use the ``save()`` Eloquent method to insert an |
| 75 | +instance of a ``Concert`` model as a MongoDB document. |
| 76 | + |
| 77 | +When the ``save()`` method succeeds, you can access the model instance on |
| 78 | +which you called the method. If the operation fails, the model instance is |
| 79 | +assigned ``null``. |
| 80 | + |
| 81 | +This example code performs the following actions: |
| 82 | + |
| 83 | +- Creates a new instance of the ``Concert`` model |
| 84 | +- Assigns string values to the ``performer`` and ``venue`` fields |
| 85 | +- Assigns a date to the ``performanceDate`` field by using the ``Carbon`` |
| 86 | + package |
| 87 | +- Inserts the document by calling the ``save()`` method |
| 88 | + |
| 89 | +.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php |
| 90 | + :language: php |
| 91 | + :dedent: |
| 92 | + :start-after: begin model insert one |
| 93 | + :end-before: end model insert one |
| 94 | + |
| 95 | +You can retrieve the inserted document's ``_id`` value by accessing the model's |
| 96 | +``id`` member as shown in the following code example: |
| 97 | + |
| 98 | +.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php |
| 99 | + :language: php |
| 100 | + :dedent: |
| 101 | + :start-after: begin inserted id |
| 102 | + :end-before: end inserted id |
| 103 | + |
| 104 | +If you enable mass assignment by defining either the ``$fillable`` or |
| 105 | +``$guarded`` attributes, you can use the Eloquent model ``create()`` method |
| 106 | +to perform the insert in a single call as shown in the following example: |
| 107 | + |
| 108 | +.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php |
| 109 | + :language: php |
| 110 | + :dedent: |
| 111 | + :start-after: begin model insert one mass assign |
| 112 | + :end-before: end model insert one mass assign |
| 113 | + |
| 114 | +To learn more about the Carbon PHP API extension, see the |
| 115 | +:github:`Carbon <briannesbitt/Carbon>` GitHub repository. |
| 116 | + |
| 117 | +Insert Multiple Documents Example |
| 118 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 119 | + |
| 120 | +This example shows how to use the ``insert()`` Eloquent method to insert |
| 121 | +multiple instances of a ``Concert`` model as MongoDB documents. This bulk |
| 122 | +insert method reduces the number of calls your application needs to make |
| 123 | +to save the documents. |
| 124 | + |
| 125 | +When the ``insert()`` method succeeds, it returns the value ``1``. If it |
| 126 | +fails, it throws an exception. |
| 127 | + |
| 128 | +The example code saves multiple models in a single call by passing them as |
| 129 | +an array to the ``insert()`` method: |
| 130 | + |
| 131 | +.. note:: |
| 132 | + |
| 133 | + This example wraps the dates in the `MongoDB\BSON\UTCDateTime <{+phplib-api+}/class.mongodb-bson-utcdatetime.php>`__ |
| 134 | + class to convert it to a type MongoDB can serialize because Laravel |
| 135 | + skips attribute casting on bulk insert operations. |
| 136 | + |
| 137 | +.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php |
| 138 | + :language: php |
| 139 | + :dedent: |
| 140 | + :start-after: begin model insert many |
| 141 | + :end-before: end model insert many |
| 142 | + |
0 commit comments