Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6b9511f

Browse files
Merge 4.2 into 4.3 (#2887)
2 parents 8355c30 + 0d68370 commit 6b9511f

File tree

3 files changed

+229
-25
lines changed

3 files changed

+229
-25
lines changed

‎docs/fundamentals/read-operations.txt‎

Lines changed: 120 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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 besieged 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

274374
Return 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.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use MongoDB\Laravel\Eloquent\Model;
6+
7+
class Movie extends Model
8+
{
9+
protected $connection = 'mongodb';
10+
protected $collection = 'movies';
11+
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot'];
12+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers;
6+
7+
use App\Models\Movie;
8+
use MongoDB\Laravel\Tests\TestCase;
9+
10+
class ReadOperationsTest extends TestCase
11+
{
12+
protected function setUp(): void
13+
{
14+
require_once __DIR__ . '/Movie.php';
15+
16+
parent::setUp();
17+
18+
Movie::truncate();
19+
Movie::insert([
20+
['year' => 2010, 'imdb' => ['rating' => 9]],
21+
['year' => 2010, 'imdb' => ['rating' => 9.5]],
22+
['year' => 2010, 'imdb' => ['rating' => 7]],
23+
['year' => 1999, 'countries' => ['Indonesia', 'Canada'], 'title' => 'Title 1'],
24+
['year' => 1999, 'countries' => ['Indonesia'], 'title' => 'Title 2'],
25+
['year' => 1999, 'countries' => ['Indonesia'], 'title' => 'Title 3'],
26+
['year' => 1999, 'countries' => ['Indonesia'], 'title' => 'Title 4'],
27+
['year' => 1999, 'countries' => ['Canada'], 'title' => 'Title 5'],
28+
['year' => 1999, 'runtime' => 30],
29+
]);
30+
}
31+
32+
/**
33+
* @runInSeparateProcess
34+
* @preserveGlobalState disabled
35+
*/
36+
public function testFindFilter(): void
37+
{
38+
// start-query
39+
$movies = Movie::where('year', 2010)
40+
->where('imdb.rating', '>', 8.5)
41+
->get();
42+
// end-query
43+
44+
$this->assertNotNull($movies);
45+
$this->assertCount(2, $movies);
46+
}
47+
48+
/**
49+
* @runInSeparateProcess
50+
* @preserveGlobalState disabled
51+
*/
52+
public function testSkipLimit(): void
53+
{
54+
// start-skip-limit
55+
$movies = Movie::where('year', 1999)
56+
->skip(2)
57+
->take(3)
58+
->get();
59+
// end-skip-limit
60+
61+
$this->assertNotNull($movies);
62+
$this->assertCount(3, $movies);
63+
}
64+
65+
/**
66+
* @runInSeparateProcess
67+
* @preserveGlobalState disabled
68+
*/
69+
public function testSort(): void
70+
{
71+
// start-sort
72+
$movies = Movie::where('countries', 'Indonesia')
73+
->orderBy('year')
74+
->orderBy('title', 'desc')
75+
->get();
76+
// end-sort
77+
78+
$this->assertNotNull($movies);
79+
$this->assertCount(4, $movies);
80+
}
81+
82+
/**
83+
* @runInSeparateProcess
84+
* @preserveGlobalState disabled
85+
*/
86+
public function testFirst(): void
87+
{
88+
// start-first
89+
$movie = Movie::where('runtime', 30)
90+
->orderBy('_id')
91+
->first();
92+
// end-first
93+
94+
$this->assertNotNull($movie);
95+
$this->assertInstanceOf(Movie::class, $movie);
96+
}
97+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /