-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35962: sorts #2879
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
DOCSP-35962: sorts #2879
Changes from 2 commits
0c44e6c
8cb7389
8ca26d0
12e6d55
0787f2b
9b63d29
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 |
---|---|---|
|
@@ -188,6 +188,8 @@ method: | |
|
||
- :ref:`laravel-skip-limit` uses the ``skip()`` method to set the number of documents | ||
to skip and the ``take()`` method to set the total number of documents to return | ||
- :ref:`laravel-sort` uses the ``orderBy()`` method to return query | ||
results in a specified order based on field values | ||
- :ref:`laravel-retrieve-one` uses the ``first()`` method to return the first document | ||
that matches the query filter | ||
|
||
|
@@ -269,6 +271,103 @@ documents. | |
Plot: A sci-fi update of the famous 6th Century poem. In a beseiged land, Beowulf must | ||
battle against the hideous creature Grendel and his vengeance seeking mother. | ||
|
||
.. _laravel-sort: | ||
|
||
Sort Query Results | ||
~~~~~~~~~~~~~~~~~~ | ||
|
||
To order query results based on the values of specified fields, use the ``where()`` method | ||
followed by the ``orderBy()`` method. You can set an **ascending** or | ||
**descending** sort direction on results. By default, the ``orderBy()`` | ||
method sets an ascending sort on the supplied field name. To specify a | ||
descending sort, pass ``'desc'`` as the second parameter. | ||
|
||
If your documents contain duplicate values in a specific field, you can | ||
handle the tie by specifying additional fields to sort on. This ensures consistent | ||
results if the additional fields contain unique values. | ||
|
||
This example queries for documents in which the value of the ``countries`` field contains | ||
``'Indonesia'`` and orders results first by an ascending sort on the | ||
``year`` field, then a descending sort on the ``title`` field. | ||
|
||
.. tabs:: | ||
|
||
.. tab:: Query Syntax | ||
:tabid: query-syntax | ||
|
||
Use the following syntax to specify the query: | ||
|
||
.. code-block:: php | ||
|
||
$movies = Movie::where('countries', 'Indonesia') | ||
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. Could you move the examples to a test? 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. I will move the query syntax examples into a test, but I'm not sure how to move the Controller method snippets into include files. 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. Ok, let's keep the controller example here for now. |
||
->orderBy('year') | ||
->orderBy('title', 'desc') | ||
->get(); | ||
|
||
.. tab:: Controller Method | ||
:tabid: controller | ||
|
||
To see the query results in the ``browse_movies`` view, edit the ``show()`` function | ||
in the ``MovieController.php`` file to resemble the following code: | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: | ||
:language: php | ||
|
||
class MovieController | ||
{ | ||
public function show() | ||
{ | ||
$movies = Movie::where('countries', 'Indonesia') | ||
->orderBy('year') | ||
->orderBy('title', 'desc') | ||
->get(); | ||
|
||
return view('browse_movies', [ | ||
'movies' => $movies | ||
]); | ||
} | ||
} | ||
|
||
.. output:: | ||
:language: none | ||
:visible: false | ||
|
||
Title: Joni's Promise | ||
Year: 2005 | ||
Runtime: 83 | ||
IMDB Rating: 7.6 | ||
IMDB Votes: 702 | ||
Plot: A film delivery man promises ... | ||
|
||
Title: Gie | ||
Year: 2005 | ||
Runtime: 147 | ||
IMDB Rating: 7.5 | ||
IMDB Votes: 470 | ||
Plot: Soe Hok Gie is an activist who lived in the sixties ... | ||
|
||
Title: Requiem from Java | ||
Year: 2006 | ||
Runtime: 120 | ||
IMDB Rating: 6.6 | ||
IMDB Votes: 316 | ||
Plot: Setyo (Martinus Miroto) and Siti (Artika Sari Dewi) | ||
are young married couple ... | ||
|
||
... | ||
|
||
.. tip:: | ||
|
||
To learn more about sorting, see the following resources: | ||
|
||
- :manual:`Natural order </reference/glossary/#std-term-natural-order>` | ||
in the Server manual glossary | ||
- `Ordering, Grouping, Limit and Offset <https://laravel.com/docs/10.x/queries#ordering-grouping-limit-and-offset>`__ | ||
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. The Laravel version in the doc url needs to be updated to 11.x for laravel-mongodb 4.2 |
||
in the Laravel documentation | ||
|
||
.. _laravel-retrieve-one: | ||
|
||
Return the First Result | ||
|
@@ -337,10 +436,5 @@ field. | |
|
||
.. tip:: | ||
|
||
To learn more about sorting, see the following resources: | ||
|
||
- :manual:`Natural order </reference/glossary/#std-term-natural-order>` | ||
in the Server manual glossary | ||
- `Ordering, Grouping, Limit and Offset <https://laravel.com/docs/10.x/queries#ordering-grouping-limit-and-offset>`__ | ||
in the Laravel documentation | ||
|
||
To learn more about the ``orderBy()`` method, see the | ||
:ref:`laravel-sort` section of this guide. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion:
I think the orderBy() method also accepts `asc' for ascending sort. It could be helpful to let the know the user exists (for completeness) and to mention that it's not necessary since it's the default value. I think keeping the asc examples as-is is fine though.