-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35974: Update one usage example #2781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
norareidy
merged 31 commits into
mongodb:4.1
from
norareidy:DOCSP-35974-update-one-usage-ex
Apr 3, 2024
Merged
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
2a01a1f
DOCSP-35974: Update one usage example
norareidy 5d5bc52
fixes
norareidy de2fb19
other usage ex changes
norareidy 0f0479c
reworking the example
norareidy 6edf281
add tip, spacing
norareidy 8bcf1a7
restructuring
norareidy a1238b0
reword tip
norareidy 30be915
fixes
norareidy 3f9ef3d
edits
norareidy 82a4f20
reword
norareidy 74db7ef
add more running info
norareidy c9e279c
small change
norareidy ae4bb21
edits
norareidy 5ceb6b3
source constant
norareidy 97187cd
test file
norareidy d9aa3c1
move test file
norareidy 0008e18
single quotes
norareidy cb10358
print syntax
norareidy 6c3d851
print statement again
norareidy 71f72e7
Merge remote-tracking branch 'upstream/4.1' into DOCSP-35974-update-o…
norareidy f3ac9c8
JT feedback
norareidy b237a86
apply phpcbf formatting
norareidy 4856810
code
norareidy 777e3cb
code fix
norareidy d67827a
apply phpcbf formatting
norareidy abab190
JT feedback
norareidy d7b03f1
Merge branch 'DOCSP-35974-update-one-usage-ex' of github.com:norareid…
norareidy aabaf2c
Merge remote-tracking branch 'upstream/4.1' into pr/2781
GromNaN 2fb18a5
Fix UpdateOneTest example
GromNaN 3f9e394
Merge remote-tracking branch 'upstream/4.1' into DOCSP-35974-update-o…
norareidy 1c90a28
add to TOC
norareidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,109 @@ | ||
.. _laravel-update-one-usage: | ||
|
||
================= | ||
Update a Document | ||
================= | ||
|
||
You can update a document in a collection by chaining the ``where()``, ``first()``, | ||
and ``update()`` methods to an Eloquent model or a query builder. | ||
|
||
Pass a query filter to the ``where()`` method and call the ``first()`` method to match | ||
only one document. Then, pass the document changes to the ``update()`` method to update | ||
one document that matches the filter. | ||
|
||
To learn more about updating data with {+odm-short+}, see the `Updates | ||
<https://laravel.com/docs/6.x/eloquent#updates>`__ section of the Laravel documentation. | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Example | ||
------- | ||
|
||
This usage example updates a document that matches a query filter from the ``movies`` | ||
collection in the ``sample_mflix`` database. The example uses the ``Movie`` Eloquent model | ||
to represent the ``movies`` collection. | ||
|
||
This example performs the following actions: | ||
|
||
- Defines a query filter to match documents in which the value of the ``title`` field | ||
is ``'Carol'`` | ||
- Calls the ``first()`` method to retrieve the first document that matches the query filter, | ||
according to the sort order specified by the ``orderBy()`` method | ||
- Calls the ``update()`` method to update the value of the ``metacritic`` field from ``98`` | ||
to ``94`` | ||
|
||
To see the update operation code, select the :guilabel:`Controller File Code` tab. To see | ||
the HTML code that specifies a view, select the :guilabel:`View File Code` tab. | ||
|
||
.. important:: | ||
|
||
Before running the update operation, you must add the following statement to the | ||
``Movie`` class in your ``Movie.php`` file: | ||
|
||
.. code-block:: php | ||
|
||
protected $fillable = array('metacritic'); | ||
|
||
The ``$fillable`` property specifies which document fields are mass-assignable. To learn | ||
more about ``$fillable``, see the `Mass Assignment <https://laravel.com/docs/4.2/eloquent#mass-assignment>`__ | ||
section of the Laravel documentation. | ||
|
||
.. tabs:: | ||
|
||
.. tab:: Controller File Code | ||
:tabid: find-one-controller | ||
|
||
.. code-block:: php | ||
|
||
class MovieController | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public function show() | ||
{ | ||
$update = Movie::where('title', 'Carol') | ||
->orderBy('_id') | ||
->first() | ||
->update(['metacritic' => 94]); | ||
|
||
return view('browse_movies', [ | ||
'updates' => $update | ||
]); | ||
} | ||
} | ||
|
||
.. tab:: View File Code | ||
:tabid: find-one-view | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: | ||
:language: php | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Browse Movies</title> | ||
</head> | ||
<body> | ||
<h2>Movies</h2> | ||
<p> | ||
Updated documents: {{ $updates }}<br> | ||
</p> | ||
</body> | ||
</html> | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
Updated documents: 1 | ||
|
||
To perform the update operation, start your Laravel application by running the following command: | ||
|
||
.. code-block:: bash | ||
|
||
php artisan serve | ||
|
||
Then, open the following URL in your web browser to view the expected output: | ||
|
||
.. code-block:: none | ||
|
||
http://127.0.0.1:8000/browse_movies |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.