diff --git a/docs/includes/usage-examples/CountTest.php b/docs/includes/usage-examples/CountTest.php index ecf53db47..5e7e34c62 100644 --- a/docs/includes/usage-examples/CountTest.php +++ b/docs/includes/usage-examples/CountTest.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Movie; +use Illuminate\Support\Facades\DB; use MongoDB\Laravel\Tests\TestCase; class CountTest extends TestCase @@ -29,14 +30,24 @@ public function testCount(): void ], ]); - // begin-count + // begin-eloquent-count $count = Movie::where('genres', 'Biography') ->count(); echo 'Number of documents: ' . $count; - // end-count + // end-eloquent-count $this->assertEquals(2, $count); - $this->expectOutputString('Number of documents: 2'); + + // begin-qb-count + $count = DB::table('movies') + ->where('genres', 'Biography') + ->count(); + + echo 'Number of documents: ' . $count; + // end-qb-count + + $this->assertEquals(2, $count); + $this->expectOutputString('Number of documents: 2Number of documents: 2'); } } diff --git a/docs/includes/usage-examples/DeleteManyTest.php b/docs/includes/usage-examples/DeleteManyTest.php index 5050f952e..8948c06fb 100644 --- a/docs/includes/usage-examples/DeleteManyTest.php +++ b/docs/includes/usage-examples/DeleteManyTest.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Movie; +use Illuminate\Support\Facades\DB; use MongoDB\Laravel\Tests\TestCase; class DeleteManyTest extends TestCase @@ -29,14 +30,35 @@ public function testDeleteMany(): void ], ]); - // begin-delete-many + // begin-eloquent-delete-many $deleted = Movie::where('year', '<=', 1910) ->delete(); echo 'Deleted documents: ' . $deleted; - // end-delete-many + // end-eloquent-delete-many $this->assertEquals(2, $deleted); - $this->expectOutputString('Deleted documents: 2'); + + Movie::insert([ + [ + 'title' => 'Train Pulling into a Station', + 'year' => 1896, + ], + [ + 'title' => 'The Ball Game', + 'year' => 1898, + ], + ]); + + // begin-qb-delete-many + $deleted = DB::table('movies') + ->where('year', '<=', 1910) + ->delete(); + + echo 'Deleted documents: ' . $deleted; + // end-qb-delete-many + + $this->assertEquals(2, $deleted); + $this->expectOutputString('Deleted documents: 2Deleted documents: 2'); } } diff --git a/docs/includes/usage-examples/DeleteOneTest.php b/docs/includes/usage-examples/DeleteOneTest.php index 1a2acd4e0..9038618f8 100644 --- a/docs/includes/usage-examples/DeleteOneTest.php +++ b/docs/includes/usage-examples/DeleteOneTest.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Movie; +use Illuminate\Support\Facades\DB; use MongoDB\Laravel\Tests\TestCase; class DeleteOneTest extends TestCase @@ -25,16 +26,33 @@ public function testDeleteOne(): void ], ]); - // begin-delete-one + // begin-eloquent-delete-one $deleted = Movie::where('title', 'Quiz Show') - ->orderBy('_id') ->limit(1) ->delete(); echo 'Deleted documents: ' . $deleted; - // end-delete-one + // end-eloquent-delete-one $this->assertEquals(1, $deleted); - $this->expectOutputString('Deleted documents: 1'); + + Movie::insert([ + [ + 'title' => 'Quiz Show', + 'runtime' => 133, + ], + ]); + + // begin-qb-delete-one + $deleted = DB::table('movies') + ->where('title', 'Quiz Show') + ->limit(1) + ->delete(); + + echo 'Deleted documents: ' . $deleted; + // end-qb-delete-one + + $this->assertEquals(1, $deleted); + $this->expectOutputString('Deleted documents: 1Deleted documents: 1'); } } diff --git a/docs/includes/usage-examples/DistinctTest.php b/docs/includes/usage-examples/DistinctTest.php index 0b7812241..35a0e63ce 100644 --- a/docs/includes/usage-examples/DistinctTest.php +++ b/docs/includes/usage-examples/DistinctTest.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Movie; +use Illuminate\Support\Facades\DB; use MongoDB\Laravel\Tests\TestCase; class DistinctTest extends TestCase @@ -45,15 +46,25 @@ public function testDistinct(): void ], ]); - // begin-distinct + // begin-eloquent-distinct $ratings = Movie::where('directors', 'Sofia Coppola') ->select('imdb.rating') ->distinct() ->get(); echo $ratings; - // end-distinct + // end-eloquent-distinct - $this->expectOutputString('[[6.4],[7.8]]'); + // begin-qb-distinct + $ratings = DB::table('movies') + ->where('directors', 'Sofia Coppola') + ->select('imdb.rating') + ->distinct() + ->get(); + + echo $ratings; + // end-qb-distinct + + $this->expectOutputString('[[6.4],[7.8]][6.4,7.8]'); } } diff --git a/docs/includes/usage-examples/FindManyTest.php b/docs/includes/usage-examples/FindManyTest.php index 18324c62d..e136c65d7 100644 --- a/docs/includes/usage-examples/FindManyTest.php +++ b/docs/includes/usage-examples/FindManyTest.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Movie; +use Illuminate\Support\Facades\DB; use MongoDB\Laravel\Tests\TestCase; class FindManyTest extends TestCase @@ -33,11 +34,20 @@ public function testFindMany(): void ], ]); - // begin-find + // begin-eloquent-find $movies = Movie::where('runtime', '>', 900) ->orderBy('_id') ->get(); - // end-find + // end-eloquent-find + + $this->assertEquals(2, $movies->count()); + + // begin-qb-find + $movies = DB::table('movies') + ->where('runtime', '>', 900) + ->orderBy('_id') + ->get(); + // end-qb-find $this->assertEquals(2, $movies->count()); } diff --git a/docs/includes/usage-examples/FindOneTest.php b/docs/includes/usage-examples/FindOneTest.php index 98452a6a6..c5304d378 100644 --- a/docs/includes/usage-examples/FindOneTest.php +++ b/docs/includes/usage-examples/FindOneTest.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Movie; +use Illuminate\Support\Facades\DB; use MongoDB\Laravel\Tests\TestCase; class FindOneTest extends TestCase @@ -13,7 +14,7 @@ class FindOneTest extends TestCase * @runInSeparateProcess * @preserveGlobalState disabled */ - public function testFindOne(): void + public function testEloquentFindOne(): void { require_once __DIR__ . '/Movie.php'; @@ -22,15 +23,41 @@ public function testFindOne(): void ['title' => 'The Shawshank Redemption', 'directors' => ['Frank Darabont', 'Rob Reiner']], ]); - // begin-find-one + // begin-eloquent-find-one $movie = Movie::where('directors', 'Rob Reiner') - ->orderBy('_id') + ->orderBy('id') ->first(); echo $movie->toJson(); - // end-find-one + // end-eloquent-find-one $this->assertInstanceOf(Movie::class, $movie); $this->expectOutputRegex('/^{"_id":"[a-z0-9]{24}","title":"The Shawshank Redemption","directors":\["Frank Darabont","Rob Reiner"\]}$/'); } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testQBFindOne(): void + { + require_once __DIR__ . '/Movie.php'; + + Movie::truncate(); + Movie::insert([ + ['title' => 'The Shawshank Redemption', 'directors' => ['Frank Darabont', 'Rob Reiner']], + ]); + + // begin-qb-find-one + $movie = DB::table('movies') + ->where('directors', 'Rob Reiner') + ->orderBy('_id') + ->first(); + + echo $movie['title']; + // end-qb-find-one + + $this->assertSame($movie['title'], 'The Shawshank Redemption'); + $this->expectOutputString('The Shawshank Redemption'); + } } diff --git a/docs/includes/usage-examples/InsertManyTest.php b/docs/includes/usage-examples/InsertManyTest.php index e1bf4539a..79e00971f 100644 --- a/docs/includes/usage-examples/InsertManyTest.php +++ b/docs/includes/usage-examples/InsertManyTest.php @@ -6,6 +6,7 @@ use App\Models\Movie; use DateTimeImmutable; +use Illuminate\Support\Facades\DB; use MongoDB\BSON\UTCDateTime; use MongoDB\Laravel\Tests\TestCase; @@ -21,7 +22,7 @@ public function testInsertMany(): void Movie::truncate(); - // begin-insert-many + // begin-eloquent-insert-many $success = Movie::insert([ [ 'title' => 'Anatomy of a Fall', @@ -38,9 +39,31 @@ public function testInsertMany(): void ]); echo 'Insert operation success: ' . ($success ? 'yes' : 'no'); - // end-insert-many + // end-eloquent-insert-many $this->assertTrue($success); - $this->expectOutputString('Insert operation success: yes'); + + // begin-qb-insert-many + $success = DB::table('movies') + ->insert([ + [ + 'title' => 'Anatomy of a Fall', + 'release_date' => new UTCDateTime(new DateTimeImmutable('2023-08-23')), + ], + [ + 'title' => 'The Boy and the Heron', + 'release_date' => new UTCDateTime(new DateTimeImmutable('2023-12-08')), + ], + [ + 'title' => 'Passages', + 'release_date' => new UTCDateTime(new DateTimeImmutable('2023-06-28')), + ], + ]); + + echo 'Insert operation success: ' . ($success ? 'yes' : 'no'); + // end-qb-insert-many + + $this->assertTrue($success); + $this->expectOutputString('Insert operation success: yesInsert operation success: yes'); } } diff --git a/docs/includes/usage-examples/InsertOneTest.php b/docs/includes/usage-examples/InsertOneTest.php index 15eadf419..821029499 100644 --- a/docs/includes/usage-examples/InsertOneTest.php +++ b/docs/includes/usage-examples/InsertOneTest.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Movie; +use Illuminate\Support\Facades\DB; use MongoDB\Laravel\Tests\TestCase; class InsertOneTest extends TestCase @@ -13,13 +14,13 @@ class InsertOneTest extends TestCase * @runInSeparateProcess * @preserveGlobalState disabled */ - public function testInsertOne(): void + public function testEloquentInsertOne(): void { require_once __DIR__ . '/Movie.php'; Movie::truncate(); - // begin-insert-one + // begin-eloquent-insert-one $movie = Movie::create([ 'title' => 'Marriage Story', 'year' => 2019, @@ -27,9 +28,34 @@ public function testInsertOne(): void ]); echo $movie->toJson(); - // end-insert-one + // end-eloquent-insert-one $this->assertInstanceOf(Movie::class, $movie); + $this->assertSame($movie->title, 'Marriage Story'); $this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","_id":"[a-z0-9]{24}"}$/'); } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testQBInsertOne(): void + { + require_once __DIR__ . '/Movie.php'; + + Movie::truncate(); + + // begin-qb-insert-one + $success = DB::table('movies') + ->insert([ + 'title' => 'Marriage Story', + 'year' => 2019, + 'runtime' => 136, + ]); + + echo 'Insert operation success: ' . ($success ? 'yes' : 'no'); + // end-qb-insert-one + + $this->expectOutputString('Insert operation success: yes'); + } } diff --git a/docs/includes/usage-examples/UpdateManyTest.php b/docs/includes/usage-examples/UpdateManyTest.php index 49a77dd95..9d4a11ac8 100644 --- a/docs/includes/usage-examples/UpdateManyTest.php +++ b/docs/includes/usage-examples/UpdateManyTest.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Movie; +use Illuminate\Support\Facades\DB; use MongoDB\Laravel\Tests\TestCase; class UpdateManyTest extends TestCase @@ -35,14 +36,41 @@ public function testUpdateMany(): void ], ]); - // begin-update-many + // begin-eloquent-update-many $updates = Movie::where('imdb.rating', '>', 9.0) ->update(['acclaimed' => true]); echo 'Updated documents: ' . $updates; - // end-update-many + // end-eloquent-update-many $this->assertEquals(2, $updates); - $this->expectOutputString('Updated documents: 2'); + + Movie::insert([ + [ + 'title' => 'ABCD', + 'imdb' => [ + 'rating' => 9.5, + 'votes' => 1, + ], + ], + [ + 'title' => 'Testing', + 'imdb' => [ + 'rating' => 9.3, + 'votes' => 1, + ], + ], + ]); + + // begin-qb-update-many + $updates = DB::table('movies') + ->where('imdb.rating', '>', 9.0) + ->update(['acclaimed' => true]); + + echo 'Updated documents: ' . $updates; + // end-qb-update-many + + $this->assertEquals(2, $updates); + $this->expectOutputString('Updated documents: 2Updated documents: 2'); } } diff --git a/docs/includes/usage-examples/UpdateOneTest.php b/docs/includes/usage-examples/UpdateOneTest.php index e1f864170..2ed356d5a 100644 --- a/docs/includes/usage-examples/UpdateOneTest.php +++ b/docs/includes/usage-examples/UpdateOneTest.php @@ -28,7 +28,7 @@ public function testUpdateOne(): void ], ]); - // begin-update-one + // begin-eloquent-update-one $updates = Movie::where('title', 'Carol') ->orderBy('_id') ->first() @@ -40,7 +40,7 @@ public function testUpdateOne(): void ]); echo 'Updated documents: ' . $updates; - // end-update-one + // end-eloquent-update-one $this->assertTrue($updates); $this->expectOutputString('Updated documents: 1'); diff --git a/docs/includes/usage-examples/operation-description.rst b/docs/includes/usage-examples/operation-description.rst index 68119a249..c68754475 100644 --- a/docs/includes/usage-examples/operation-description.rst +++ b/docs/includes/usage-examples/operation-description.rst @@ -1,2 +1,3 @@ -|operator-description| by creating a query builder, using a method such -as ``Model::where()`` or the ``DB`` facade to match documents in a collection, and then calling |result-operation|. +|operator-description| by using a method such as ``Model::where()`` or +methods from the ``DB`` facade to match documents, and then calling +|result-operation|. diff --git a/docs/query-builder.txt b/docs/query-builder.txt index 649cdde34..990c1005c 100644 --- a/docs/query-builder.txt +++ b/docs/query-builder.txt @@ -631,7 +631,7 @@ a query: :end-before: end options The query builder accepts the same options that you can set for -the :phpmethod:`find() ` method in the +the :phpmethod:`MongoDB\Collection::find()` method in the {+php-library+}. Some of the options to modify query results, such as ``skip``, ``sort``, and ``limit``, are settable directly as query builder methods and are described in the diff --git a/docs/usage-examples.txt b/docs/usage-examples.txt index 87a87df88..14478c004 100644 --- a/docs/usage-examples.txt +++ b/docs/usage-examples.txt @@ -43,6 +43,10 @@ operations. Each usage example includes the following components: - Example code that you can run from an application controller - Output displayed by the print statement +To learn more about the operations demonstrated in the usage examples, +see the :ref:`laravel-fundamentals-read-ops` and +:ref:`laravel-fundamentals-write-ops` guides. + How to Use the Usage Examples ----------------------------- diff --git a/docs/usage-examples/count.txt b/docs/usage-examples/count.txt index c3af477ee..aadd1e0c6 100644 --- a/docs/usage-examples/count.txt +++ b/docs/usage-examples/count.txt @@ -25,35 +25,79 @@ Count Documents .. replacement:: result-operation - the ``count()`` method to retrieve the results. + the ``count()`` method to retrieve the results Example ------- -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Counts the documents from the ``movies`` collection that match a query filter -- Prints the matching document count - -The example calls the following methods on the ``Movie`` model: - -- ``where()``: Matches documents in which the value of the ``genres`` field includes ``"Biography"``. -- ``count()``: Counts the number of matching documents. This method returns an integer value. - -.. io-code-block:: - - .. input:: ../includes/usage-examples/CountTest.php - :start-after: begin-count - :end-before: end-count - :language: php - :dedent: - - .. output:: - :language: console - :visible: false - - Number of documents: 1267 +Select from the following :guilabel:`Eloquent` and :guilabel:`Query +Builder` tabs to view usage examples for the same operation that use +each corresponding query syntax: + +.. tabs:: + + .. tab:: Eloquent + :tabid: eloquent-model-count + + This example performs the following actions: + + - Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database + - Counts the documents from the ``movies`` collection that match a + query filter + - Prints the matching document count + + The example calls the following methods on the ``Movie`` model: + + - ``where()``: Matches documents in which the value of the + ``genres`` field includes ``"Biography"`` + - ``count()``: Counts the number of matching documents and returns + the count as an integer + + .. io-code-block:: + + .. input:: ../includes/usage-examples/CountTest.php + :start-after: begin-eloquent-count + :end-before: end-eloquent-count + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Number of documents: 1267 + + .. tab:: Query Builder + :tabid: query-builder-count + + This example performs the following actions: + + - Accesses the ``movies`` collection by calling the ``table()`` + method from the ``DB`` facade + - Counts the documents from the ``movies`` collection that match a + query filter + - Prints the matching document count + + The example calls the following query builder methods: + + - ``where()``: Matches documents in which the value of the + ``genres`` field includes ``"Biography"`` + - ``count()``: Counts the number of matching documents and returns + the count as an integer + + .. io-code-block:: + + .. input:: ../includes/usage-examples/CountTest.php + :start-after: begin-qb-count + :end-before: end-qb-count + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Number of documents: 1267 .. include:: /includes/usage-examples/fact-edit-laravel-app.rst diff --git a/docs/usage-examples/deleteMany.txt b/docs/usage-examples/deleteMany.txt index cf8680184..22cc8d183 100644 --- a/docs/usage-examples/deleteMany.txt +++ b/docs/usage-examples/deleteMany.txt @@ -17,48 +17,91 @@ Delete Multiple Documents :depth: 1 :class: singlecol -You can delete multiple documents in a collection by calling the ``delete()`` method on an -object collection or a query builder. +You can delete multiple documents in a collection by calling the +``delete()`` method on an object collection or a query builder. -To delete multiple documents, pass a query filter to the ``where()`` method. Then, delete the -matching documents by calling the ``delete()`` method. +To delete multiple documents, pass a query filter to the ``where()`` +method. Then, delete the matching documents by calling the ``delete()`` +method. -Example -------- - -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Deletes documents from the ``movies`` collection that match a query filter -- Prints the number of deleted documents - -The example calls the following methods on the ``Movie`` model: - -- ``where()``: matches documents in which the value of the ``year`` field is less than or - equal to ``1910``. -- ``delete()``: deletes the matched documents. This method returns the number - of documents that the method successfully deletes. - -.. io-code-block:: - :copyable: true +.. tip:: - .. input:: ../includes/usage-examples/DeleteManyTest.php - :start-after: begin-delete-many - :end-before: end-delete-many - :language: php - :dedent: + To learn more about deleting documents with the {+odm-short+}, see + the :ref:`laravel-fundamentals-delete-documents` section of the Write + Operations guide. - .. output:: - :language: console - :visible: false +Example +------- - Deleted documents: 7 +Select from the following :guilabel:`Eloquent` and :guilabel:`Query +Builder` tabs to view usage examples for the same operation that use +each corresponding query syntax: + +.. tabs:: + + .. tab:: Eloquent + :tabid: eloquent-model-count + + This example performs the following actions: + + - Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database + - Deletes documents from the ``movies`` collection that match a + query filter + - Prints the number of deleted documents + + The example calls the following methods on the ``Movie`` model: + + - ``where()``: Matches documents in which the value of the + ``year`` field is less than or equal to ``1910`` + - ``delete()``: Deletes the matched documents and returns the + number of documents successfully deleted + + .. io-code-block:: + :copyable: true + + .. input:: ../includes/usage-examples/DeleteManyTest.php + :start-after: begin-eloquent-delete-many + :end-before: end-eloquent-delete-many + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Deleted documents: 7 + + .. tab:: Query Builder + :tabid: query-builder-count + + This example performs the following actions: + + - Accesses the ``movies`` collection by calling the ``table()`` + method from the ``DB`` facade + - Deletes documents from the ``movies`` collection that match a + query filter + - Prints the number of deleted documents + + The example calls the following query builder methods: + + - ``where()``: Matches documents in which the value of the + ``year`` field is less than or equal to ``1910`` + - ``delete()``: Deletes the matched documents and returns the + number of documents successfully deleted + + .. io-code-block:: + + .. input:: ../includes/usage-examples/DeleteManyTest.php + :start-after: begin-qb-delete-many + :end-before: end-qb-delete-many + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Deleted documents: 7 .. include:: /includes/usage-examples/fact-edit-laravel-app.rst - -.. tip:: - - To learn more about deleting documents with the {+odm-short+}, see the :ref:`laravel-fundamentals-delete-documents` - section of the Write Operations guide. - diff --git a/docs/usage-examples/deleteOne.txt b/docs/usage-examples/deleteOne.txt index 1298255da..8a88c0241 100644 --- a/docs/usage-examples/deleteOne.txt +++ b/docs/usage-examples/deleteOne.txt @@ -17,50 +17,93 @@ Delete a Document :depth: 1 :class: singlecol -You can delete a document in a collection by retrieving a single Eloquent model and calling -the ``delete()`` method, or by calling ``delete()`` directly on a query builder. +You can delete a document in a collection by retrieving a single +Eloquent model and calling the ``delete()`` method, or by calling +``delete()`` directly on a query builder. -To delete a document, pass a query filter to the ``where()`` method, sort the matching documents, -and call the ``limit()`` method to retrieve only the first document. Then, delete this matching -document by calling the ``delete()`` method. +To delete a document, pass a query filter to the ``where()`` method, +sort the matching documents, and call the ``limit()`` method to retrieve +only the first document. Then, delete this matching document by calling +the ``delete()`` method. -Example -------- - -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Deletes a document from the ``movies`` collection that matches a query filter -- Prints the number of deleted documents - -The example calls the following methods on the ``Movie`` model: - -- ``where()``: matches documents in which the value of the ``title`` field is ``"Quiz Show"`` -- ``orderBy()``: sorts matched documents by their ascending ``_id`` values -- ``limit()``: retrieves only the first matching document -- ``delete()``: deletes the retrieved document - -.. io-code-block:: - :copyable: true +.. tip:: - .. input:: ../includes/usage-examples/DeleteOneTest.php - :start-after: begin-delete-one - :end-before: end-delete-one - :language: php - :dedent: + To learn more about deleting documents with the {+odm-short+}, see + the :ref:`laravel-fundamentals-delete-documents` section of the Write + Operations guide. - .. output:: - :language: console - :visible: false +Example +------- - Deleted documents: 1 +Select from the following :guilabel:`Eloquent` and :guilabel:`Query +Builder` tabs to view usage examples for the same operation that use +each corresponding query syntax: + +.. tabs:: + + .. tab:: Eloquent + :tabid: eloquent-model-count + + This example performs the following actions: + + - Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database + - Deletes a document from the ``movies`` collection that matches a + query filter + - Prints the number of deleted documents + + The example calls the following methods on the ``Movie`` model: + + - ``where()``: Matches documents in which the value of the + ``title`` field is ``"Quiz Show"`` + - ``limit()``: Retrieves only the first matching document + - ``delete()``: Deletes the retrieved document + + .. io-code-block:: + :copyable: true + + .. input:: ../includes/usage-examples/DeleteOneTest.php + :start-after: begin-eloquent-delete-one + :end-before: end-eloquent-delete-one + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Deleted documents: 1 + + .. tab:: Query Builder + :tabid: query-builder-count + + This example performs the following actions: + + - Accesses the ``movies`` collection by calling the ``table()`` + method from the ``DB`` facade + - Deletes a document from the ``movies`` collection that matches a + query filter + - Prints the number of deleted documents + + The example calls the following query builder methods: + + - ``where()``: Matches documents in which the value of the + ``title`` field is ``"Quiz Show"`` + - ``limit()``: Retrieves only the first matching document + - ``delete()``: Deletes the retrieved document + + .. io-code-block:: + + .. input:: ../includes/usage-examples/DeleteOneTest.php + :start-after: begin-qb-delete-one + :end-before: end-qb-delete-one + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Deleted documents: 1 .. include:: /includes/usage-examples/fact-edit-laravel-app.rst - -.. tip:: - - To learn more about deleting documents with the {+odm-short+}, see the `Deleting Models - `__ section of the - Laravel documentation. - diff --git a/docs/usage-examples/distinct.txt b/docs/usage-examples/distinct.txt index 5d62ec8be..cfe1e4644 100644 --- a/docs/usage-examples/distinct.txt +++ b/docs/usage-examples/distinct.txt @@ -17,50 +17,99 @@ Retrieve Distinct Field Values :depth: 1 :class: singlecol -You can retrieve distinct field values of documents in a collection by calling the ``distinct()`` -method on an object collection or a query builder. +You can retrieve distinct field values of documents in a collection by +calling the ``distinct()`` method on an object collection or a query +builder. -To retrieve distinct field values, pass a query filter to the ``where()`` method and a field name -to the ``select()`` method. Then, call ``distinct()`` to return the unique values of the selected -field in documents that match the query filter. +To retrieve distinct field values, pass a query filter to the +``where()`` method and a field name to the ``select()`` method. Then, +call ``distinct()`` to return the unique values of the selected field in +documents that match the query filter. -Example -------- - -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Retrieves distinct field values of documents from the ``movies`` collection that match a query filter -- Prints the distinct values - -The example calls the following methods on the ``Movie`` model: - -- ``where()``: matches documents in which the value of the ``directors`` field includes ``"Sofia Coppola"``. -- ``select()``: retrieves the matching documents' ``imdb.rating`` field values. -- ``distinct()``: retrieves the unique values of the selected field and returns - the list of values. -- ``get()``: retrieves the query results. - -.. io-code-block:: - :copyable: true +.. tip:: - .. input:: ../includes/usage-examples/DistinctTest.php - :start-after: begin-distinct - :end-before: end-distinct - :language: php - :dedent: + For more information about query filters, see the + :ref:`laravel-retrieve-matching` section of the Read Operations + guide. - .. output:: - :language: console - :visible: false +Example +------- - [[5.6],[6.4],[7.2],[7.8]] +Select from the following :guilabel:`Eloquent` and :guilabel:`Query +Builder` tabs to view usage examples for the same operation that use +each corresponding query syntax: + +.. tabs:: + + .. tab:: Eloquent + :tabid: eloquent-model-count + + This example performs the following actions: + + - Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database + - Retrieves distinct field values of documents from the ``movies`` + collection that match a query filter + - Prints the distinct values + + The example calls the following methods on the ``Movie`` model: + + - ``where()``: Matches documents in which the value of the + ``directors`` field includes ``"Sofia Coppola"`` + - ``select()``: Retrieves the matching documents' ``imdb.rating`` + field values + - ``distinct()``: Retrieves the unique values of the selected + field and returns the list of values + - ``get()``: Retrieves the query results + + .. io-code-block:: + :copyable: true + + .. input:: ../includes/usage-examples/DistinctTest.php + :start-after: begin-eloquent-distinct + :end-before: end-eloquent-distinct + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + [[5.6],[6.4],[7.2],[7.8]] + + .. tab:: Query Builder + :tabid: query-builder-count + + This example performs the following actions: + + - Accesses the ``movies`` collection by calling the ``table()`` + method from the ``DB`` facade + - Retrieves distinct field values of documents from the ``movies`` + collection that match a query filter + - Prints the distinct values + + The example calls the following query builder methods: + + - ``where()``: Matches documents in which the value of the + ``directors`` field includes ``"Sofia Coppola"`` + - ``select()``: Retrieves the matching documents' ``imdb.rating`` + field values + - ``distinct()``: Retrieves the unique values of the selected + field and returns the list of values + - ``get()``: Retrieves the query results + + .. io-code-block:: + + .. input:: ../includes/usage-examples/DistinctTest.php + :start-after: begin-qb-distinct + :end-before: end-qb-distinct + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + [5.6,6.4,7.2,7.8] .. include:: /includes/usage-examples/fact-edit-laravel-app.rst - -.. tip:: - - For more information about query filters, see the :ref:`laravel-retrieve-matching` section of - the Read Operations guide. - diff --git a/docs/usage-examples/find.txt b/docs/usage-examples/find.txt index 957ece537..187676392 100644 --- a/docs/usage-examples/find.txt +++ b/docs/usage-examples/find.txt @@ -25,66 +25,115 @@ Find Multiple Documents .. replacement:: result-operation - the ``get()`` method to retrieve the results. + the ``get()`` method to retrieve the results Pass a query filter to the ``where()`` method to retrieve documents that meet a set of criteria. When you call the ``get()`` method, MongoDB returns the -matching documents according to their :term:`natural order` in the database or +matching documents according to their :term:`natural order` in the collection or according to the sort order that you can specify by using the ``orderBy()`` method. -To learn more about query builder methods, see the :ref:`laravel-query-builder` -guide. +.. tip:: + + To learn about other ways to retrieve documents with the + {+odm-short+}, see the :ref:`laravel-fundamentals-retrieve` guide. Example ------- -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Retrieves and prints documents from the ``movies`` collection that match a query filter - -The example calls the following methods on the ``Movie`` model: - -- ``where()``: matches documents in which the value of the ``runtime`` field is greater than ``900`` -- ``orderBy()``: sorts matched documents by their ascending ``_id`` values -- ``get()``: retrieves the query results as a Laravel collection object - -.. io-code-block:: - :copyable: true - - .. input:: ../includes/usage-examples/FindManyTest.php - :start-after: begin-find - :end-before: end-find - :language: php - :dedent: - - .. output:: - :language: json - :visible: false - - // Results are truncated - - [ - { - "_id": ..., - "runtime": 1256, - "title": "Centennial", - ..., - }, - { - "_id": ..., - "runtime": 1140, - "title": "Baseball", - ..., - }, - ... - ] +Select from the following :guilabel:`Eloquent` and :guilabel:`Query +Builder` tabs to view usage examples for the same operation that use +each corresponding query syntax: + +.. tabs:: + + .. tab:: Eloquent + :tabid: eloquent-model-count + + This example performs the following actions: + + - Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database + - Retrieves and prints documents from the ``movies`` collection + that match a query filter + + The example calls the following methods on the ``Movie`` model: + + - ``where()``: Matches documents in which the value of the + ``runtime`` field is greater than ``900`` + - ``orderBy()``: Sorts matched documents by their ascending + ``_id`` values + - ``get()``: Retrieves the query results as a Laravel collection + object + + .. io-code-block:: + :copyable: true + + .. input:: ../includes/usage-examples/FindManyTest.php + :start-after: begin-eloquent-find + :end-before: end-eloquent-find + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + // Results are truncated + + [ + { + "_id": ..., + "runtime": 1256, + "title": "Centennial", + ..., + }, + { + "_id": ..., + "runtime": 1140, + "title": "Baseball", + ..., + }, + ... + ] + + .. tab:: Query Builder + :tabid: query-builder-count + + This example performs the following actions: + + - Accesses the ``movies`` collection by calling the ``table()`` + method from the ``DB`` facade + - Retrieves and prints documents from the ``movies`` collection + that match a query filter + + The example calls the following query builder methods: + + - ``where()``: Matches documents in which the value of the + ``runtime`` field is greater than ``900`` + - ``orderBy()``: Sorts matched documents by their ascending + ``_id`` values + - ``get()``: Retrieves the query results as a Laravel collection + object + + .. io-code-block:: + + .. input:: ../includes/usage-examples/FindManyTest.php + :start-after: begin-qb-find + :end-before: end-qb-find + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + // Results are truncated + + Illuminate\Support\Collection Object ( [items:protected] => + Array ( [0] => Array ( [_id] => ... [runtime] => 1256 + [title] => Centennial [1] => Array + ( [_id] => ... [runtime] => 1140 + [title] => Baseball ) ... .. include:: /includes/usage-examples/fact-edit-laravel-app.rst - -.. tip:: - - To learn about other ways to retrieve documents with the {+odm-short+}, see the - :ref:`laravel-fundamentals-retrieve` guide. diff --git a/docs/usage-examples/findOne.txt b/docs/usage-examples/findOne.txt index aa0e035f1..d5df8aae1 100644 --- a/docs/usage-examples/findOne.txt +++ b/docs/usage-examples/findOne.txt @@ -19,52 +19,97 @@ Find a Document .. replacement:: result-operation - the ``first()`` method to return one document. + the ``first()`` method to return one document -If multiple documents match the query filter, ``first()`` returns the first matching document according to the documents' -:term:`natural order` in the database or according to the sort order that you can specify -by using the ``orderBy()`` method. +If multiple documents match the query filter, ``first()`` returns the +first matching document according to the documents' :term:`natural +order` in the database or according to the sort order that you can +specify by using the ``orderBy()`` method. -Example -------- - -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Retrieves a document from the ``movies`` collection that matches a query filter -- Prints the retrieved document - -The example calls the following methods on the ``Movie`` model: - -- ``where()``: matches documents in which the value of the ``directors`` field includes ``"Rob Reiner"``. -- ``orderBy()``: sorts matched documents by their ascending ``_id`` values. -- ``first()``: retrieves only the first matching document. - -.. io-code-block:: - - .. input:: ../includes/usage-examples/FindOneTest.php - :start-after: begin-find-one - :end-before: end-find-one - :language: php - :dedent: +.. tip:: - .. output:: - :language: console - :visible: false + To learn about other ways to retrieve documents with the + {+odm-short+}, see the :ref:`laravel-fundamentals-retrieve` guide. - // Result is truncated +Example +------- - { - "_id": ..., - "title": "This Is Spinal Tap", - "directors": [ "Rob Reiner" ], - ... - } +Select from the following :guilabel:`Eloquent` and :guilabel:`Query +Builder` tabs to view usage examples for the same operation that use +each corresponding query syntax: + +.. tabs:: + + .. tab:: Eloquent + :tabid: eloquent-model-count + + This example performs the following actions: + + - Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database + - Retrieves a document from the ``movies`` collection that matches + a query filter + - Prints the retrieved document + + The example calls the following methods on the ``Movie`` model: + + - ``where()``: Matches documents in which the value of the + ``directors`` field includes ``"Rob Reiner"`` + - ``orderBy()``: Sorts matched documents by their ascending ``_id`` values + - ``first()``: Retrieves only the first matching document + + .. io-code-block:: + :copyable: true + + .. input:: ../includes/usage-examples/FindOneTest.php + :start-after: begin-eloquent-find-one + :end-before: end-eloquent-find-one + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + // Result is truncated + + { + "_id": ..., + "title": "This Is Spinal Tap", + "directors": [ "Rob Reiner" ], + ... + } + + .. tab:: Query Builder + :tabid: query-builder-count + + This example performs the following actions: + + - Accesses the ``movies`` collection by calling the ``table()`` + method from the ``DB`` facade + - Retrieves a document from the ``movies`` collection that matches + a query filter + - Prints the ``title`` field of the retrieved document + + The example calls the following query builder methods: + + - ``where()``: Matches documents in which the value of the + ``directors`` field includes ``"Rob Reiner"`` + - ``orderBy()``: Sorts matched documents by their ascending ``_id`` values + - ``first()``: Retrieves only the first matching document + + .. io-code-block:: + + .. input:: ../includes/usage-examples/FindOneTest.php + :start-after: begin-qb-find-one + :end-before: end-qb-find-one + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + This Is Spinal Tap .. include:: /includes/usage-examples/fact-edit-laravel-app.rst - -.. tip:: - - To learn more about retrieving documents with the {+odm-short+}, see the - :ref:`laravel-fundamentals-retrieve` guide. diff --git a/docs/usage-examples/insertMany.txt b/docs/usage-examples/insertMany.txt index 2d59a78ab..48acfe17e 100644 --- a/docs/usage-examples/insertMany.txt +++ b/docs/usage-examples/insertMany.txt @@ -24,40 +24,78 @@ To insert multiple documents, call the ``insert()`` method and specify the new d as an array inside the method call. Each array entry contains a single document's field values. -Example -------- - -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Inserts documents into the ``movies`` collection -- Prints whether the insert operation succeeds - -The example calls the ``insert()`` method to insert documents that contain -information about movies released in ``2023``. If the insert operation is -successful, it returns a value of ``1``. If the operation fails, it throws -an exception. - -.. io-code-block:: - :copyable: true +.. tip:: - .. input:: ../includes/usage-examples/InsertManyTest.php - :start-after: begin-insert-many - :end-before: end-insert-many - :language: php - :dedent: + To learn more about insert operations, see the + :ref:`laravel-fundamentals-insert-documents` section + of the Write Operations guide. - .. output:: - :language: console - :visible: false +Example +------- - Insert operation success: yes +Select from the following :guilabel:`Eloquent` and :guilabel:`Query +Builder` tabs to view usage examples for the same operation that use +each corresponding query syntax: + +.. tabs:: + + .. tab:: Eloquent + :tabid: eloquent-model-count + + This example performs the following actions: + + - Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database + - Inserts documents into the ``movies`` collection + - Prints whether the insert operation succeeds + + The example calls the ``insert()`` method to insert documents that represent + movies released in ``2023``. If the insert operation is + successful, it returns a value of ``1``. If the operation fails, it throws + an exception. + + .. io-code-block:: + :copyable: true + + .. input:: ../includes/usage-examples/InsertManyTest.php + :start-after: begin-eloquent-insert-many + :end-before: end-eloquent-insert-many + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Insert operation success: yes + + .. tab:: Query Builder + :tabid: query-builder-count + + This example performs the following actions: + + - Accesses the ``movies`` collection by calling the ``table()`` + method from the ``DB`` facade + - Inserts documents into the ``movies`` collection + - Prints whether the insert operation succeeds + + The example calls the ``insert()`` method to insert documents that represent + movies released in ``2023``. If the insert operation is + successful, it returns a value of ``1``. If the operation fails, it throws + an exception. + + .. io-code-block:: + + .. input:: ../includes/usage-examples/InsertManyTest.php + :start-after: begin-qb-insert-many + :end-before: end-qb-insert-many + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Insert operation success: yes .. include:: /includes/usage-examples/fact-edit-laravel-app.rst - -.. tip:: - - To learn more about insert operations, see the :ref:`laravel-fundamentals-insert-documents` section - of the Write Operations guide. - diff --git a/docs/usage-examples/insertOne.txt b/docs/usage-examples/insertOne.txt index e28e12090..1a246ab72 100644 --- a/docs/usage-examples/insertOne.txt +++ b/docs/usage-examples/insertOne.txt @@ -23,50 +23,90 @@ an Eloquent model or query builder. To insert a document, pass the data you need to insert as a document containing the fields and values to the ``create()`` method. -Example -------- - -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Inserts a document into the ``movies`` collection - -The example calls the ``create()`` method to insert a document that contains the following -information: - -- ``title`` value of ``"Marriage Story"`` -- ``year`` value of ``2019`` -- ``runtime`` value of ``136`` - -.. io-code-block:: - :copyable: true +.. tip:: - .. input:: ../includes/usage-examples/InsertOneTest.php - :start-after: begin-insert-one - :end-before: end-insert-one - :language: php - :dedent: + You can also use the ``save()`` or ``insert()`` methods to insert a + document into a collection. To learn more about insert operations, + see the :ref:`laravel-fundamentals-insert-documents` section of the + Write Operations guide. - .. output:: - :language: json - :visible: false +Example +------- - { - "title": "Marriage Story", - "year": 2019, - "runtime": 136, - "updated_at": "...", - "created_at": "...", - "_id": "..." - } +Select from the following :guilabel:`Eloquent` and :guilabel:`Query +Builder` tabs to view usage examples for the same operation that use +each corresponding query syntax: + +.. tabs:: + + .. tab:: Eloquent + :tabid: eloquent-model-count + + This example performs the following actions: + + - Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database + - Inserts a document into the ``movies`` collection + - Prints the newly inserted document + + The example calls the ``create()`` method to insert a document + that contains the following fields and values: + + - ``title`` value of ``"Marriage Story"`` + - ``year`` value of ``2019`` + - ``runtime`` value of ``136`` + + .. io-code-block:: + :copyable: true + + .. input:: ../includes/usage-examples/InsertOneTest.php + :start-after: begin-eloquent-insert-one + :end-before: end-eloquent-insert-one + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + { + "title": "Marriage Story", + "year": 2019, + "runtime": 136, + "updated_at": "...", + "created_at": "...", + "_id": "..." + } + + .. tab:: Query Builder + :tabid: query-builder-count + + This example performs the following actions: + + - Accesses the ``movies`` collection by calling the ``table()`` + method from the ``DB`` facade + - Inserts a document into the ``movies`` collection + - Prints whether the insert operation succeeds + + The example calls the ``insert()`` method to insert a document + that contains the following fields and values: + + - ``title`` value of ``"Marriage Story"`` + - ``year`` value of ``2019`` + - ``runtime`` value of ``136`` + + .. io-code-block:: + + .. input:: ../includes/usage-examples/InsertOneTest.php + :start-after: begin-qb-insert-one + :end-before: end-qb-insert-one + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Insert operation success: yes .. include:: /includes/usage-examples/fact-edit-laravel-app.rst - -.. tip:: - - You can also use the ``save()`` or ``insert()`` methods to insert a document into a collection. - To learn more about insert operations, see the :ref:`laravel-fundamentals-insert-documents` section - of the Write Operations guide. - - diff --git a/docs/usage-examples/updateMany.txt b/docs/usage-examples/updateMany.txt index 89c262da7..c2c83ce1c 100644 --- a/docs/usage-examples/updateMany.txt +++ b/docs/usage-examples/updateMany.txt @@ -24,43 +24,85 @@ Pass a query filter to the ``where()`` method to retrieve documents that meet a set of criteria. Then, update the matching documents by passing your intended document changes to the ``update()`` method. -Example -------- - -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Updates documents from the ``movies`` collection that match a query filter -- Prints the number of updated documents - -The example calls the following methods on the ``Movie`` model: - -- ``where()``: matches documents in which the value of the ``imdb.rating`` nested field - is greater than ``9.0``. -- ``update()``: updates the matching documents by adding an ``acclaimed`` field and setting - its value to ``true``. This method returns the number of documents that were successfully - updated. - -.. io-code-block:: - :copyable: true - - .. input:: ../includes/usage-examples/UpdateManyTest.php - :start-after: begin-update-many - :end-before: end-update-many - :language: php - :dedent: - - .. output:: - :language: console - :visible: false - - Updated documents: 20 - -.. include:: /includes/usage-examples/fact-edit-laravel-app.rst - .. tip:: To learn more about updating data with the {+odm-short+}, see the :ref:`laravel-fundamentals-modify-documents` section of the Write Operations guide. +Example +------- + +Select from the following :guilabel:`Eloquent` and :guilabel:`Query +Builder` tabs to view usage examples for the same operation that use +each corresponding query syntax: + +.. tabs:: + + .. tab:: Eloquent + :tabid: eloquent-model-count + + This example performs the following actions: + + - Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database + - Updates documents from the ``movies`` collection that match a + query filter + - Prints the number of updated documents + + The example calls the following methods on the ``Movie`` model: + + - ``where()``: Matches documents in which the value of the + ``imdb.rating`` nested field is greater than ``9.0`` + - ``update()``: Updates the matching documents by adding an + ``acclaimed`` field and setting its value to ``true``, then + returns the number of updated documents + + .. io-code-block:: + :copyable: true + + .. input:: ../includes/usage-examples/UpdateManyTest.php + :start-after: begin-eloquent-update-many + :end-before: end-eloquent-update-many + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Updated documents: 20 + + .. tab:: Query Builder + :tabid: query-builder-count + + This example performs the following actions: + + - Accesses the ``movies`` collection by calling the ``table()`` + method from the ``DB`` facade + - Updates documents from the ``movies`` collection that match a + query filter + - Prints the number of updated documents + + The example calls the following query builder methods: + + - ``where()``: Matches documents in which the value of the + ``imdb.rating`` nested field is greater than ``9.0`` + - ``update()``: Updates the matching documents by adding an + ``acclaimed`` field and setting its value to ``true``, then + returns the number of updated documents + + .. io-code-block:: + + .. input:: ../includes/usage-examples/UpdateManyTest.php + :start-after: begin-qb-update-many + :end-before: end-qb-update-many + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Updated documents: 20 + +.. include:: /includes/usage-examples/fact-edit-laravel-app.rst diff --git a/docs/usage-examples/updateOne.txt b/docs/usage-examples/updateOne.txt index ecdc8982d..785ba3b09 100644 --- a/docs/usage-examples/updateOne.txt +++ b/docs/usage-examples/updateOne.txt @@ -17,37 +17,46 @@ Update a Document :depth: 1 :class: singlecol -You can update a document in a collection by retrieving a single document and calling -the ``update()`` method on an Eloquent model or a query builder. +You can update a document in a collection by retrieving a single +document and calling the ``update()`` method on an Eloquent model. -Pass a query filter to the ``where()`` method, sort the matching documents, and call the -``first()`` method to retrieve only the first document. Then, update this matching document -by passing your intended document changes to the ``update()`` method. +Pass a query filter to the ``where()`` method, sort the matching +documents, and call the ``first()`` method to retrieve only the first +document. Then, update this matching document by passing your intended +document changes to the ``update()`` method. + +.. tip:: + + To learn more about updating data with the {+odm-short+}, see the :ref:`laravel-fundamentals-modify-documents` + section of the Write Operations guide. Example ------- -This usage example performs the following actions: - -- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the - ``sample_mflix`` database -- Updates a document from the ``movies`` collection that matches the query filter +This example performs the following actions: + +- Uses the ``Movie`` Eloquent model to represent the ``movies`` + collection in the ``sample_mflix`` database +- Updates a document from the ``movies`` collection that matches + the query filter - Prints the number of updated documents The example calls the following methods on the ``Movie`` model: -- ``where()``: matches documents in which the value of the ``title`` field is ``"Carol"``. -- ``orderBy()``: sorts matched documents by their ascending ``_id`` values. -- ``first()``: retrieves only the first matching document. -- ``update()``: updates the value of the ``imdb.rating`` nested field to from ``6.9`` to - ``7.3`` and the value of the ``imdb.votes`` nested field from ``493`` to ``142000``. +- ``where()``: Matches documents in which the value of the + ``title`` field is ``"Carol"`` +- ``orderBy()``: Sorts matched documents by their ascending ``_id`` values +- ``first()``: Retrieves only the first matching document +- ``update()``: Updates the value of the ``imdb.rating`` nested + field to from ``6.9`` to ``7.3`` and the value of the + ``imdb.votes`` nested field from ``493`` to ``142000`` .. io-code-block:: :copyable: true .. input:: ../includes/usage-examples/UpdateOneTest.php - :start-after: begin-update-one - :end-before: end-update-one + :start-after: begin-eloquent-update-one + :end-before: end-eloquent-update-one :language: php :dedent: @@ -58,9 +67,3 @@ The example calls the following methods on the ``Movie`` model: Updated documents: 1 .. include:: /includes/usage-examples/fact-edit-laravel-app.rst - -.. tip:: - - To learn more about updating data with the {+odm-short+}, see the :ref:`laravel-fundamentals-modify-documents` - section of the Write Operations guide. -