-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOCSP-35974: Update one usage example (#2781)
* DOCSP-35974: Update one usage example * fixes * other usage ex changes * reworking the example * add tip, spacing * restructuring * reword tip * fixes * edits * reword * add more running info * small change * edits * source constant * test file * move test file * single quotes * print syntax * print statement again * JT feedback * apply phpcbf formatting * code * apply phpcbf formatting * JT feedback * Fix UpdateOneTest example * add to TOC --------- Co-authored-by: norareidy <[email protected]> Co-authored-by: Jérôme Tamarelle <[email protected]>
- Loading branch information
1 parent
9a616f9
commit 8eb9271
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use MongoDB\Laravel\Eloquent\Model; | ||
|
||
class Movie extends Model | ||
{ | ||
protected $connection = 'mongodb'; | ||
protected $collection = 'movies'; | ||
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Movie; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class UpdateOneTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testUpdateOne(): void | ||
{ | ||
require_once __DIR__ . '/Movie.php'; | ||
|
||
Movie::truncate(); | ||
Movie::insert([ | ||
[ | ||
'title' => 'Carol', | ||
'imdb' => [ | ||
'rating' => 7.2, | ||
'votes' => 125000, | ||
], | ||
], | ||
]); | ||
|
||
// begin-update-one | ||
$updates = Movie::where('title', 'Carol') | ||
->orderBy('_id') | ||
->first() | ||
->update([ | ||
'imdb' => [ | ||
'rating' => 7.3, | ||
'votes' => 142000, | ||
], | ||
]); | ||
|
||
echo 'Updated documents: ' . $updates; | ||
// end-update-one | ||
|
||
$this->assertTrue($updates); | ||
$this->expectOutputString('Updated documents: 1'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
.. _laravel-update-one-usage: | ||
|
||
================= | ||
Update a Document | ||
================= | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: update one, modify, code example | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
: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. | ||
|
||
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. | ||
|
||
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 a query filter. | ||
|
||
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``. This method also updates 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 | ||
|
||
For instructions on editing your Laravel application to run the usage example, see the | ||
:ref:`Usage Example landing page <laravel-usage-examples>`. | ||
|
||
.. tip:: | ||
|
||
To learn more about updating data with {+odm-short+}, see the `Updates | ||
<https://laravel.com/docs/{+laravel-docs-version+}/eloquent#updates>`__ section of the | ||
Laravel documentation. | ||
|