Skip to content

Commit 87aaade

Browse files
Merge 4.1 into 4.2 (#2871)
2 parents 524d159 + 1a6b596 commit 87aaade

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 DistinctTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
* @preserveGlobalState disabled
15+
*/
16+
public function testDistinct(): void
17+
{
18+
require_once __DIR__ . '/Movie.php';
19+
20+
Movie::truncate();
21+
Movie::insert([
22+
[
23+
'title' => 'Marie Antoinette',
24+
'directors' => ['Sofia Coppola'],
25+
'imdb' => [
26+
'rating' => 6.4,
27+
'votes' => 74350,
28+
],
29+
],
30+
[
31+
'title' => 'Somewhere',
32+
'directors' => ['Sofia Coppola'],
33+
'imdb' => [
34+
'rating' => 6.4,
35+
'votes' => 33753,
36+
],
37+
],
38+
[
39+
'title' => 'Lost in Translation',
40+
'directors' => ['Sofia Coppola'],
41+
'imdb' => [
42+
'rating' => 7.8,
43+
'votes' => 298747,
44+
],
45+
],
46+
]);
47+
48+
// begin-distinct
49+
$ratings = Movie::where('directors', 'Sofia Coppola')
50+
->select('imdb.rating')
51+
->distinct()
52+
->get();
53+
54+
echo $ratings;
55+
// end-distinct
56+
57+
$this->expectOutputString('[[6.4],[7.8]]');
58+
}
59+
}

docs/usage-examples.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,5 @@ calls the controller function and returns the result to a web interface.
7676
/usage-examples/deleteOne
7777
/usage-examples/deleteMany
7878
/usage-examples/count
79+
/usage-examples/distinct
80+

docs/usage-examples/distinct.txt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.. _laravel-distinct-usage:
2+
3+
==============================
4+
Retrieve Distinct Field Values
5+
==============================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: unique, different, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 1
18+
:class: singlecol
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.
22+
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.
26+
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()``: accesses the unique values of the ``imdb.rating`` field among the matching
42+
documents. This method returns a list of values.
43+
- ``get()``: retrieves the query results.
44+
45+
.. io-code-block::
46+
:copyable: true
47+
48+
.. input:: ../includes/usage-examples/DistinctTest.php
49+
:start-after: begin-distinct
50+
:end-before: end-distinct
51+
:language: php
52+
:dedent:
53+
54+
.. output::
55+
:language: console
56+
:visible: false
57+
58+
[[5.6],[6.4],[7.2],[7.8]]
59+
60+
To learn how to edit your Laravel application to run the usage example, see the
61+
:ref:`Usage Examples landing page <laravel-usage-examples>`.
62+
63+
.. tip::
64+
65+
For more information about query filters, see the :ref:`laravel-retrieve-matching` section of
66+
the Read Operations guide.
67+

0 commit comments

Comments
 (0)