diff --git a/docs/includes/usage-examples/FindOneTest.php b/docs/includes/usage-examples/FindOneTest.php index 42510a4af..014611b85 100644 --- a/docs/includes/usage-examples/FindOneTest.php +++ b/docs/includes/usage-examples/FindOneTest.php @@ -32,7 +32,7 @@ public function testFindOne(): void // 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"\]}$/'); + $this->assertSame($movie->title, 'The Shawshank Redemption'); // begin-qb-find-one $movie = DB::table('movies') @@ -40,7 +40,7 @@ public function testFindOne(): void ->orderBy('_id') ->first(); - echo $movie[0]->toJson(); + echo print_r($movie); // end-qb-find-one } } diff --git a/docs/includes/usage-examples/InsertOneTest.php b/docs/includes/usage-examples/InsertOneTest.php index 15eadf419..33f0e4d00 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 @@ -19,7 +20,7 @@ public function testInsertOne(): void Movie::truncate(); - // begin-insert-one + // begin-eloquent-insert-one $movie = Movie::create([ 'title' => 'Marriage Story', 'year' => 2019, @@ -27,9 +28,20 @@ public function testInsertOne(): void ]); echo $movie->toJson(); - // end-insert-one + // end-eloquent-insert-one + + // 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->assertInstanceOf(Movie::class, $movie); - $this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","_id":"[a-z0-9]{24}"}$/'); + $this->assertSame($movie->title, 'Marriage Story'); } } diff --git a/docs/includes/usage-examples/UpdateManyTest.php b/docs/includes/usage-examples/UpdateManyTest.php index 49a77dd95..897afe706 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,23 @@ 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'); + + // 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->expectOutputString('Updated documents: 2Updated documents: 0'); } } diff --git a/docs/includes/usage-examples/UpdateOneTest.php b/docs/includes/usage-examples/UpdateOneTest.php index e1f864170..847643ff5 100644 --- a/docs/includes/usage-examples/UpdateOneTest.php +++ b/docs/includes/usage-examples/UpdateOneTest.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\Movie; +use Illuminate\Support\Facades\DB; use MongoDB\Laravel\Tests\TestCase; class UpdateOneTest extends TestCase @@ -28,7 +29,7 @@ public function testUpdateOne(): void ], ]); - // begin-update-one + // begin-eloquent-update-one $updates = Movie::where('title', 'Carol') ->orderBy('_id') ->first() @@ -40,9 +41,25 @@ public function testUpdateOne(): void ]); echo 'Updated documents: ' . $updates; - // end-update-one + // end-eloquent-update-one $this->assertTrue($updates); - $this->expectOutputString('Updated documents: 1'); + + // begin-qb-update-one + $updates = DB::table('movies') + ->where('title', 'Carol') + ->orderBy('_id') + ->first() + ->update([ + 'imdb' => [ + 'rating' => 7.3, + 'votes' => 142000, + ], + ]); + + echo 'Updated documents: ' . $updates; + // end-qb-update-one + + $this->expectOutputString('Updated documents: 1Updated documents: 0'); } } diff --git a/docs/usage-examples/insertOne.txt b/docs/usage-examples/insertOne.txt index e28e12090..5bd059840 100644 --- a/docs/usage-examples/insertOne.txt +++ b/docs/usage-examples/insertOne.txt @@ -23,50 +23,88 @@ 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 + + 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 + + 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:: + + .. 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..9470b4a5d 100644 --- a/docs/usage-examples/updateOne.txt +++ b/docs/usage-examples/updateOne.txt @@ -24,43 +24,89 @@ Pass a query filter to the ``where()`` method, sort the matching documents, and ``first()`` method to retrieve only the first document. Then, update this matching document 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 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``. - -.. io-code-block:: - :copyable: true - - .. input:: ../includes/usage-examples/UpdateOneTest.php - :start-after: begin-update-one - :end-before: end-update-one - :language: php - :dedent: - - .. output:: - :language: console - :visible: false - - 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. +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 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`` + + .. io-code-block:: + :copyable: true + + .. input:: ../includes/usage-examples/UpdateOneTest.php + :start-after: begin-eloquent-update-one + :end-before: end-eloquent-update-one + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Updated 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 + - Updates a document from the ``movies`` collection that matches + the 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 + ``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:: + + .. input:: ../includes/usage-examples/UpdateOneTest.php + :start-after: begin-qb-update-one + :end-before: end-qb-update-one + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Updated documents: 1 + +.. include:: /includes/usage-examples/fact-edit-laravel-app.rst