@@ -94,11 +94,11 @@ retrieve documents that meet the following criteria:
9494
9595 Use the following syntax to specify the query:
9696
97- .. code-block :: php
98-
99- $movies = Movie::where('year', 2010)
100- ->where('imdb.rating', '>', 8.5)
101- ->get();
97+ .. literalinclude :: /includes/fundamentals/read-operations/ReadOperationsTest. php
98+ :language: php
99+ :dedent:
100+ :start-after: start-query
101+ :end-before: end-query
102102
103103 .. tab:: Controller Method
104104 :tabid: controller
@@ -188,6 +188,8 @@ method:
188188
189189- :ref:`laravel-skip-limit` uses the ``skip()`` method to set the number of documents
190190 to skip and the ``take()`` method to set the total number of documents to return
191+ - :ref:`laravel-sort` uses the ``orderBy()`` method to return query
192+ results in a specified order based on field values
191193- :ref:`laravel-retrieve-one` uses the ``first()`` method to return the first document
192194 that matches the query filter
193195
@@ -207,12 +209,11 @@ documents.
207209
208210 Use the following syntax to specify the query:
209211
210- .. code-block:: php
211-
212- $movies = Movie::where('year', 1999)
213- ->skip(2)
214- ->take(3)
215- ->get();
212+ .. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
213+ :language: php
214+ :dedent:
215+ :start-after: start-skip-limit
216+ :end-before: end-skip-limit
216217
217218 .. tab:: Controller Method
218219 :tabid: controller
@@ -269,6 +270,105 @@ documents.
269270 Plot: A sci-fi update of the famous 6th Century poem. In a beseiged land, Beowulf must
270271 battle against the hideous creature Grendel and his vengeance seeking mother.
271272
273+ .. _laravel-sort:
274+
275+ Sort Query Results
276+ ~~~~~~~~~~~~~~~~~~
277+
278+ To order query results based on the values of specified fields, use the ``where()`` method
279+ followed by the ``orderBy()`` method.
280+
281+ You can set an **ascending** or **descending** sort direction on
282+ results. By default, the ``orderBy()`` method sets an ascending sort on
283+ the supplied field name, but you can explicitly specify an ascending
284+ sort by passing ``'asc'`` as the second parameter. To
285+ specify a descending sort, pass ``'desc'`` as the second parameter.
286+
287+ If your documents contain duplicate values in a specific field, you can
288+ handle the tie by specifying additional fields to sort on. This ensures consistent
289+ results if the additional fields contain unique values.
290+
291+ This example queries for documents in which the value of the ``countries`` field contains
292+ ``'Indonesia'`` and orders results first by an ascending sort on the
293+ ``year`` field, then a descending sort on the ``title`` field.
294+
295+ .. tabs::
296+
297+ .. tab:: Query Syntax
298+ :tabid: query-syntax
299+
300+ Use the following syntax to specify the query:
301+
302+ .. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
303+ :language: php
304+ :dedent:
305+ :start-after: start-sort
306+ :end-before: end-sort
307+
308+ .. tab:: Controller Method
309+ :tabid: controller
310+
311+ To see the query results in the ``browse_movies`` view, edit the ``show()`` function
312+ in the ``MovieController.php`` file to resemble the following code:
313+
314+ .. io-code-block::
315+ :copyable: true
316+
317+ .. input::
318+ :language: php
319+
320+ class MovieController
321+ {
322+ public function show()
323+ {
324+ $movies = Movie::where('countries', 'Indonesia')
325+ ->orderBy('year')
326+ ->orderBy('title', 'desc')
327+ ->get();
328+
329+ return view('browse_movies', [
330+ 'movies' => $movies
331+ ]);
332+ }
333+ }
334+
335+ .. output::
336+ :language: none
337+ :visible: false
338+
339+ Title: Joni's Promise
340+ Year: 2005
341+ Runtime: 83
342+ IMDB Rating: 7.6
343+ IMDB Votes: 702
344+ Plot: A film delivery man promises ...
345+
346+ Title: Gie
347+ Year: 2005
348+ Runtime: 147
349+ IMDB Rating: 7.5
350+ IMDB Votes: 470
351+ Plot: Soe Hok Gie is an activist who lived in the sixties ...
352+
353+ Title: Requiem from Java
354+ Year: 2006
355+ Runtime: 120
356+ IMDB Rating: 6.6
357+ IMDB Votes: 316
358+ Plot: Setyo (Martinus Miroto) and Siti (Artika Sari Dewi)
359+ are young married couple ...
360+
361+ ...
362+
363+ .. tip::
364+
365+ To learn more about sorting, see the following resources:
366+
367+ - :manual:`Natural order </reference/glossary/#std-term-natural-order>`
368+ in the Server manual glossary
369+ - `Ordering, Grouping, Limit and Offset <https://laravel.com/docs/queries#ordering-grouping-limit-and-offset>`__
370+ in the Laravel documentation
371+
272372.. _laravel-retrieve-one:
273373
274374Return the First Result
@@ -292,11 +392,11 @@ field.
292392
293393 Use the following syntax to specify the query:
294394
295- .. code-block :: php
296-
297- $movies = Movie::where('runtime', 30)
298- ->orderBy('_id')
299- -> first();
395+ .. literalinclude :: /includes/fundamentals/read-operations/ReadOperationsTest. php
396+ :language: php
397+ :dedent:
398+ :start-after: start-first
399+ :end-before: end- first
300400
301401 .. tab:: Controller Method
302402 :tabid: controller
@@ -314,12 +414,12 @@ field.
314414 {
315415 public function show()
316416 {
317- $movies = Movie::where('runtime', 30)
417+ $movie = Movie::where('runtime', 30)
318418 ->orderBy('_id')
319419 ->first();
320420
321421 return view('browse_movies', [
322- 'movies' => $movies
422+ 'movies' => $movie
323423 ]);
324424 }
325425 }
@@ -337,10 +437,5 @@ field.
337437
338438.. tip::
339439
340- To learn more about sorting, see the following resources:
341-
342- - :manual:`Natural order </reference/glossary/#std-term-natural-order>`
343- in the Server manual glossary
344- - `Ordering, Grouping, Limit and Offset <https://laravel.com/docs/10.x/queries#ordering-grouping-limit-and-offset>`__
345- in the Laravel documentation
346-
440+ To learn more about the ``orderBy()`` method, see the
441+ :ref:`laravel-sort` section of this guide.
0 commit comments