Skip to content

DOCSP-35970: Find one usage example #2768

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 32 commits into from
Apr 4, 2024
Merged
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ffd9d2c
DOCSP-35970: Find one usage example
norareidy Mar 12, 2024
e92a891
wording edits
norareidy Mar 13, 2024
bf46030
add running instructions
norareidy Mar 13, 2024
6c12f4d
spacing, wording
norareidy Mar 14, 2024
e2145b6
RR feedback
norareidy Mar 15, 2024
e9aa9de
RR feedback 2
norareidy Mar 18, 2024
4c4684d
Merge remote-tracking branch 'upstream/4.1' into DOCSP-35970-find-one…
norareidy Mar 18, 2024
3bcad19
removing tabs, explain view
norareidy Mar 18, 2024
1dc60a6
small edit
norareidy Mar 18, 2024
ab116ce
adding info
norareidy Mar 18, 2024
f6cc811
updates
norareidy Mar 18, 2024
871f721
small fixes
norareidy Mar 18, 2024
70c7014
wording change
norareidy Mar 18, 2024
c6c6e8f
test file
norareidy Mar 25, 2024
c68a476
edits
norareidy Mar 25, 2024
7b2f1dc
moving test file
norareidy Mar 25, 2024
ac545b6
print syntax
norareidy Mar 26, 2024
6b1f418
output format
norareidy Mar 26, 2024
b3c921e
Merge remote-tracking branch 'upstream/4.1' into DOCSP-35970-find-one…
norareidy Mar 26, 2024
35e1dcc
apply phpcbf formatting
norareidy Mar 26, 2024
f5f4a44
code fix
norareidy Mar 26, 2024
c11f0cd
Merge branch 'DOCSP-35970-find-one-usage' of github.com:norareidy/lar…
norareidy Mar 26, 2024
5f58f70
assertion
norareidy Mar 28, 2024
d775d8b
apply phpcbf formatting
norareidy Mar 28, 2024
5bf1b71
method name
norareidy Mar 28, 2024
fc6f15b
Merge branch 'DOCSP-35970-find-one-usage' of github.com:norareidy/lar…
norareidy Mar 28, 2024
bf98e98
Update example test
GromNaN Apr 2, 2024
420e60a
Merge remote-tracking branch 'upstream/4.1' into DOCSP-35970-find-one…
norareidy Apr 3, 2024
901a4e4
Merge branch 'DOCSP-35970-find-one-usage' of github.com:norareidy/lar…
norareidy Apr 3, 2024
a8a6558
add to TOC
norareidy Apr 3, 2024
b75a786
merging
norareidy Apr 3, 2024
4d13dc6
Fix namespace
GromNaN Apr 4, 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
115 changes: 115 additions & 0 deletions docs/usage-examples/findOne.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
.. _laravel-find-one-usage:

===============
Find a Document
===============

You can retrieve a single document from a collection by calling the ``where()`` and
``first()`` methods on an Eloquent model or a query builder.

Pass a query filter to the ``where()`` method and call the ``first()`` method to return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Pass a query filter to the ``where()`` method and call the ``first()`` method to return
Pass a query filter to the ``where()`` method and then call the ``first()`` method to return

one document in the collection that matches the filter. 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 specified by the
``orderBy()`` method.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:term:`natural order` in the database or according to the sort order specified by the
``orderBy()`` method.
:term:`natural order` in the database or according to the sort order that you can specify by using the
``orderBy()`` method.


To learn more about retrieving documents, see the :ref:`laravel-fundamentals-retrieve` guide.

Example
-------

This usage example retrieves 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 that matches documents in which the value of the ``directors`` field
is ``"Rob Reiner"``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: I think the directors field is an array-valued field

Suggested change
- Defines a query filter that matches documents in which the value of the ``directors`` field
is ``"Rob Reiner"``
- Defines a query filter that matches documents in which the value of the ``directors`` field
includes ``"Rob Reiner"``

- Retrieves the first document that matches the query filter, according to the sort order
specified by the ``orderBy()`` method

To see the find 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.

.. tabs::

.. tab:: Controller File Code
:tabid: find-one-controller

.. io-code-block::
:copyable: true

.. input::
:language: php

class MovieController
{
public function show()
{
$movie = Movie::where('directors', 'Rob Reiner')
->orderBy('_id')
->first();

return view('browse_movies', [
'movie' => $movie
]);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: move into files and include (applies to all code examples)


.. output::
:language: console
:visible: false

Title: This Is Spinal Tap
Year: 1984
Runtime: 82
Director: Rob Reiner
Plot: Spinal Tap, one of England's loudest bands, is chronicled by film director
Marty DeBergi on what proves to be a fateful tour.

.. 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>
Title: {{ $movie->title }}<br>
Year: {{ $movie->year }}<br>
Runtime: {{ $movie->runtime }}<br>
Director: {{ $movie->directors[0] }}<br>
Plot: {{ $movie->plot }}<br>
</p>
</body>
</html>

.. output::
:language: console
:visible: false

Title: This Is Spinal Tap
Year: 1984
Runtime: 82
Director: Rob Reiner
Plot: Spinal Tap, one of England's loudest bands, is chronicled by film director
Marty DeBergi on what proves to be a fateful tour.

To perform the find operation, start your Laravel application by running the following command:

.. code-block:: bash

php artisan serve

Then, open the URL http://127.0.0.1:8000/browse_movies in your web browser to view the expected
output.