@@ -17,50 +17,99 @@ Retrieve Distinct Field Values
17
17
:depth: 1
18
18
:class: singlecol
19
19
20
- You can retrieve distinct field values of documents in a collection by calling the ``distinct()``
21
- method on an object collection or a query builder.
20
+ You can retrieve distinct field values of documents in a collection by
21
+ calling the ``distinct()`` method on an object collection or a query
22
+ builder.
22
23
23
- To retrieve distinct field values, pass a query filter to the ``where()`` method and a field name
24
- to the ``select()`` method. Then, call ``distinct()`` to return the unique values of the selected
25
- field in documents that match the query filter.
24
+ To retrieve distinct field values, pass a query filter to the
25
+ ``where()`` method and a field name to the ``select()`` method. Then,
26
+ call ``distinct()`` to return the unique values of the selected field in
27
+ documents that match the query filter.
26
28
27
- Example
28
- -------
29
-
30
- This usage example performs the following actions:
31
-
32
- - Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
33
- ``sample_mflix`` database
34
- - Retrieves distinct field values of documents from the ``movies`` collection that match a query filter
35
- - Prints the distinct values
36
-
37
- The example calls the following methods on the ``Movie`` model:
38
-
39
- - ``where()``: matches documents in which the value of the ``directors`` field includes ``"Sofia Coppola"``.
40
- - ``select()``: retrieves the matching documents' ``imdb.rating`` field values.
41
- - ``distinct()``: retrieves the unique values of the selected field and returns
42
- the list of values.
43
- - ``get()``: retrieves the query results.
44
-
45
- .. io-code-block::
46
- :copyable: true
29
+ .. tip::
47
30
48
- .. input:: ../includes/usage-examples/DistinctTest.php
49
- :start-after: begin-distinct
50
- :end-before: end-distinct
51
- :language: php
52
- :dedent:
31
+ For more information about query filters, see the
32
+ :ref:`laravel-retrieve-matching` section of the Read Operations
33
+ guide.
53
34
54
- .. output::
55
- :language: console
56
- :visible: false
35
+ Example
36
+ -------
57
37
58
- [[5.6],[6.4],[7.2],[7.8]]
38
+ Select from the following :guilabel:`Eloquent` and :guilabel:`Query
39
+ Builder` tabs to view usage examples for the same operation that use
40
+ each corresponding query syntax:
41
+
42
+ .. tabs::
43
+
44
+ .. tab:: Eloquent
45
+ :tabid: eloquent-model-count
46
+
47
+ This example performs the following actions:
48
+
49
+ - Uses the ``Movie`` Eloquent model to represent the ``movies``
50
+ collection in the ``sample_mflix`` database
51
+ - Retrieves distinct field values of documents from the ``movies``
52
+ collection that match a query filter
53
+ - Prints the distinct values
54
+
55
+ The example calls the following methods on the ``Movie`` model:
56
+
57
+ - ``where()``: Matches documents in which the value of the
58
+ ``directors`` field includes ``"Sofia Coppola"``
59
+ - ``select()``: Retrieves the matching documents' ``imdb.rating``
60
+ field values
61
+ - ``distinct()``: Retrieves the unique values of the selected
62
+ field and returns the list of values
63
+ - ``get()``: Retrieves the query results
64
+
65
+ .. io-code-block::
66
+ :copyable: true
67
+
68
+ .. input:: ../includes/usage-examples/DistinctTest.php
69
+ :start-after: begin-eloquent-distinct
70
+ :end-before: end-eloquent-distinct
71
+ :language: php
72
+ :dedent:
73
+
74
+ .. output::
75
+ :language: console
76
+ :visible: false
77
+
78
+ [[5.6],[6.4],[7.2],[7.8]]
79
+
80
+ .. tab:: Query Builder
81
+ :tabid: query-builder-count
82
+
83
+ This example performs the following actions:
84
+
85
+ - Accesses the ``movies`` collection by calling the ``table()``
86
+ method from the ``DB`` facade
87
+ - Retrieves distinct field values of documents from the ``movies``
88
+ collection that match a query filter
89
+ - Prints the distinct values
90
+
91
+ The example calls the following query builder methods:
92
+
93
+ - ``where()``: Matches documents in which the value of the
94
+ ``directors`` field includes ``"Sofia Coppola"``
95
+ - ``select()``: Retrieves the matching documents' ``imdb.rating``
96
+ field values
97
+ - ``distinct()``: Retrieves the unique values of the selected
98
+ field and returns the list of values
99
+ - ``get()``: Retrieves the query results
100
+
101
+ .. io-code-block::
102
+
103
+ .. input:: ../includes/usage-examples/DistinctTest.php
104
+ :start-after: begin-qb-distinct
105
+ :end-before: end-qb-distinct
106
+ :language: php
107
+ :dedent:
108
+
109
+ .. output::
110
+ :language: console
111
+ :visible: false
112
+
113
+ [[5.6],[6.4],[7.2],[7.8]]
59
114
60
115
.. include:: /includes/usage-examples/fact-edit-laravel-app.rst
61
-
62
- .. tip::
63
-
64
- For more information about query filters, see the :ref:`laravel-retrieve-matching` section of
65
- the Read Operations guide.
66
-
0 commit comments