Skip to content

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
merged 31 commits into from
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 Mar 13, 2024
5d5bc52
fixes
norareidy Mar 13, 2024
de2fb19
other usage ex changes
norareidy Mar 15, 2024
0f0479c
reworking the example
norareidy Mar 19, 2024
6edf281
add tip, spacing
norareidy Mar 19, 2024
8bcf1a7
restructuring
norareidy Mar 20, 2024
a1238b0
reword tip
norareidy Mar 20, 2024
30be915
fixes
norareidy Mar 20, 2024
3f9ef3d
edits
norareidy Mar 20, 2024
82a4f20
reword
norareidy Mar 20, 2024
74db7ef
add more running info
norareidy Mar 21, 2024
c9e279c
small change
norareidy Mar 21, 2024
ae4bb21
edits
norareidy Mar 22, 2024
5ceb6b3
source constant
norareidy Mar 22, 2024
97187cd
test file
norareidy Mar 25, 2024
d9aa3c1
move test file
norareidy Mar 25, 2024
0008e18
single quotes
norareidy Mar 25, 2024
cb10358
print syntax
norareidy Mar 26, 2024
6c3d851
print statement again
norareidy Mar 26, 2024
71f72e7
Merge remote-tracking branch 'upstream/4.1' into DOCSP-35974-update-o…
norareidy Mar 26, 2024
f3ac9c8
JT feedback
norareidy Mar 26, 2024
b237a86
apply phpcbf formatting
norareidy Mar 26, 2024
4856810
code
norareidy Mar 26, 2024
777e3cb
code fix
norareidy Mar 26, 2024
d67827a
apply phpcbf formatting
norareidy Mar 26, 2024
abab190
JT feedback
norareidy Mar 28, 2024
d7b03f1
Merge branch 'DOCSP-35974-update-one-usage-ex' of github.com:norareid…
norareidy Mar 28, 2024
aabaf2c
Merge remote-tracking branch 'upstream/4.1' into pr/2781
GromNaN Apr 2, 2024
2fb18a5
Fix UpdateOneTest example
GromNaN Apr 2, 2024
3f9e394
Merge remote-tracking branch 'upstream/4.1' into DOCSP-35974-update-o…
norareidy Apr 3, 2024
1c90a28
add to TOC
norareidy Apr 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions docs/usage-examples/updateOne.txt
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.

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
{
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