-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 4 commits
ffd9d2c
e92a891
bf46030
6c12f4d
e2145b6
e9aa9de
4c4684d
3bcad19
1dc60a6
ab116ce
f6cc811
871f721
70c7014
c6c6e8f
c68a476
7b2f1dc
ac545b6
6b1f418
b3c921e
35e1dcc
f5f4a44
c11f0cd
5f58f70
d775d8b
5bf1b71
fc6f15b
bf98e98
420e60a
901a4e4
a8a6558
b75a786
4d13dc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
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"`` | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: I think the
Suggested change
|
||||||||||
- 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 | ||||||||||
]); | ||||||||||
} | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.