Skip to content

Commit a69ed4b

Browse files
committed
wip
1 parent 111b290 commit a69ed4b

File tree

8 files changed

+323
-135
lines changed

8 files changed

+323
-135
lines changed

docs/includes/usage-examples/DeleteManyTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public function testDeleteMany(): void
3737
echo 'Deleted documents: ' . $deleted;
3838
// end-eloquent-delete-many
3939

40+
$this->assertEquals(2, $deleted);
41+
$this->expectOutputString('Deleted documents: 2');
42+
4043
// begin-qb-delete-many
4144
$deleted = DB::table('movies')
4245
->where('year', '<=', 1910)
4346
->delete();
4447

4548
echo 'Deleted documents: ' . $deleted;
4649
// end-qb-delete-many
47-
48-
$this->assertEquals(2, $deleted);
49-
$this->expectOutputString('Deleted documents: 2');
5050
}
5151
}

docs/includes/usage-examples/DeleteOneTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public function testDeleteOne(): void
3535
echo 'Deleted documents: ' . $deleted;
3636
// end-eloquent-delete-one
3737

38+
$this->assertEquals(1, $deleted);
39+
$this->expectOutputString('Deleted documents: 1');
40+
3841
// begin-qb-delete-one
3942
$deleted = DB::table('movies')
4043
->where('title', 'Quiz Show')
@@ -44,8 +47,5 @@ public function testDeleteOne(): void
4447

4548
echo 'Deleted documents: ' . $deleted;
4649
// end-qb-delete-one
47-
48-
$this->assertEquals(1, $deleted);
49-
$this->expectOutputString('Deleted documents: 1');
5050
}
5151
}

docs/includes/usage-examples/FindManyTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Http\Controllers;
66

77
use App\Models\Movie;
8+
use Illuminate\Support\Facades\DB;
89
use MongoDB\Laravel\Tests\TestCase;
910

1011
class FindManyTest extends TestCase
@@ -33,11 +34,18 @@ public function testFindMany(): void
3334
],
3435
]);
3536

36-
// begin-find
37+
// begin-eloquent-find
3738
$movies = Movie::where('runtime', '>', 900)
3839
->orderBy('_id')
3940
->get();
40-
// end-find
41+
// end-eloquent-find
42+
43+
// begin-qb-find
44+
$movies = DB::table('movies')
45+
->where('runtime', '>', 900)
46+
->orderBy('_id')
47+
->get();
48+
// end-qb-find
4149

4250
$this->assertEquals(2, $movies->count());
4351
}

docs/includes/usage-examples/FindOneTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Http\Controllers;
66

77
use App\Models\Movie;
8+
use Illuminate\Support\Facades\DB;
89
use MongoDB\Laravel\Tests\TestCase;
910

1011
class FindOneTest extends TestCase
@@ -22,15 +23,24 @@ public function testFindOne(): void
2223
['title' => 'The Shawshank Redemption', 'directors' => ['Frank Darabont', 'Rob Reiner']],
2324
]);
2425

25-
// begin-find-one
26+
// begin-eloquent-find-one
2627
$movie = Movie::where('directors', 'Rob Reiner')
2728
->orderBy('_id')
2829
->first();
2930

3031
echo $movie->toJson();
31-
// end-find-one
32+
// end-eloquent-find-one
3233

3334
$this->assertInstanceOf(Movie::class, $movie);
3435
$this->expectOutputRegex('/^{"_id":"[a-z0-9]{24}","title":"The Shawshank Redemption","directors":\["Frank Darabont","Rob Reiner"\]}$/');
36+
37+
// begin-qb-find-one
38+
$movie = DB::table('movies')
39+
->where('directors', 'Rob Reiner')
40+
->orderBy('_id')
41+
->first();
42+
43+
echo $movie->toJson();
44+
// end-qb-find-one
3545
}
3646
}

docs/includes/usage-examples/InsertManyTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Models\Movie;
88
use DateTimeImmutable;
9+
use Illuminate\Support\Facades\DB;
910
use MongoDB\BSON\UTCDateTime;
1011
use MongoDB\Laravel\Tests\TestCase;
1112

@@ -21,7 +22,7 @@ public function testInsertMany(): void
2122

2223
Movie::truncate();
2324

24-
// begin-insert-many
25+
// begin-eloquent-insert-many
2526
$success = Movie::insert([
2627
[
2728
'title' => 'Anatomy of a Fall',
@@ -38,7 +39,27 @@ public function testInsertMany(): void
3839
]);
3940

4041
echo 'Insert operation success: ' . ($success ? 'yes' : 'no');
41-
// end-insert-many
42+
// end-eloquent-insert-many
43+
44+
// begin-qb-insert-many
45+
$success = DB::table('movies')
46+
->insert([
47+
[
48+
'title' => 'Anatomy of a Fall',
49+
'release_date' => new UTCDateTime(new DateTimeImmutable('2023-08-23')),
50+
],
51+
[
52+
'title' => 'The Boy and the Heron',
53+
'release_date' => new UTCDateTime(new DateTimeImmutable('2023-12-08')),
54+
],
55+
[
56+
'title' => 'Passages',
57+
'release_date' => new UTCDateTime(new DateTimeImmutable('2023-06-28')),
58+
],
59+
]);
60+
61+
echo 'Insert operation success: ' . ($success ? 'yes' : 'no');
62+
// end-qb-insert-many
4263

4364
$this->assertTrue($success);
4465
$this->expectOutputString('Insert operation success: yes');

docs/usage-examples/find.txt

Lines changed: 109 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,121 @@ Find Multiple Documents
2929

3030
Pass a query filter to the ``where()`` method to retrieve documents that meet a
3131
set of criteria. When you call the ``get()`` method, MongoDB returns the
32-
matching documents according to their :term:`natural order` in the database or
32+
matching documents according to their :term:`natural order` in the collection or
3333
according to the sort order that you can specify by using the ``orderBy()``
3434
method.
3535

36-
To learn more about query builder methods, see the :ref:`laravel-query-builder`
37-
guide.
36+
.. tip::
37+
38+
To learn about other ways to retrieve documents with the
39+
{+odm-short+}, see the :ref:`laravel-fundamentals-retrieve` guide.
3840

3941
Example
4042
-------
4143

42-
This usage example performs the following actions:
43-
44-
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
45-
``sample_mflix`` database
46-
- Retrieves and prints documents from the ``movies`` collection that match a query filter
47-
48-
The example calls the following methods on the ``Movie`` model:
49-
50-
- ``where()``: matches documents in which the value of the ``runtime`` field is greater than ``900``
51-
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values
52-
- ``get()``: retrieves the query results as a Laravel collection object
53-
54-
.. io-code-block::
55-
:copyable: true
56-
57-
.. input:: ../includes/usage-examples/FindManyTest.php
58-
:start-after: begin-find
59-
:end-before: end-find
60-
:language: php
61-
:dedent:
62-
63-
.. output::
64-
:language: json
65-
:visible: false
66-
67-
// Results are truncated
68-
69-
[
70-
{
71-
"_id": ...,
72-
"runtime": 1256,
73-
"title": "Centennial",
74-
...,
75-
},
76-
{
77-
"_id": ...,
78-
"runtime": 1140,
79-
"title": "Baseball",
80-
...,
81-
},
82-
...
83-
]
44+
Select from the following :guilabel:`Eloquent` and :guilabel:`Query
45+
Builder` tabs to view usage examples for the same operation that use
46+
each corresponding query syntax:
47+
48+
.. tabs::
49+
50+
.. tab:: Eloquent
51+
:tabid: eloquent-model-count
52+
53+
This example performs the following actions:
54+
55+
- Uses the ``Movie`` Eloquent model to represent the ``movies``
56+
collection in the ``sample_mflix`` database
57+
- Retrieves and prints documents from the ``movies`` collection
58+
that match a query filter
59+
60+
The example calls the following methods on the ``Movie`` model:
61+
62+
- ``where()``: Matches documents in which the value of the
63+
``runtime`` field is greater than ``900``
64+
- ``orderBy()``: Sorts matched documents by their ascending
65+
``_id`` values
66+
- ``get()``: Retrieves the query results as a Laravel collection
67+
object
68+
69+
.. io-code-block::
70+
:copyable: true
71+
72+
.. input:: ../includes/usage-examples/FindManyTest.php
73+
:start-after: begin-eloquent-find
74+
:end-before: end-eloquent-find
75+
:language: php
76+
:dedent:
77+
78+
.. output::
79+
:language: console
80+
:visible: false
81+
82+
// Results are truncated
83+
84+
[
85+
{
86+
"_id": ...,
87+
"runtime": 1256,
88+
"title": "Centennial",
89+
...,
90+
},
91+
{
92+
"_id": ...,
93+
"runtime": 1140,
94+
"title": "Baseball",
95+
...,
96+
},
97+
...
98+
]
99+
100+
.. tab:: Query Builder
101+
:tabid: query-builder-count
102+
103+
This example performs the following actions:
104+
105+
- Accesses the ``movies`` collection by calling the ``table()``
106+
method from the ``DB`` facade
107+
- Retrieves and prints documents from the ``movies`` collection
108+
that match a query filter
109+
110+
The example calls the following query builder methods:
111+
112+
- ``where()``: Matches documents in which the value of the
113+
``runtime`` field is greater than ``900``
114+
- ``orderBy()``: Sorts matched documents by their ascending
115+
``_id`` values
116+
- ``get()``: Retrieves the query results as a Laravel collection
117+
object
118+
119+
.. io-code-block::
120+
121+
.. input:: ../includes/usage-examples/FindManyTest.php
122+
:start-after: begin-qb-find
123+
:end-before: end-qb-find
124+
:language: php
125+
:dedent:
126+
127+
.. output::
128+
:language: console
129+
:visible: false
130+
131+
// Results are truncated
132+
133+
[
134+
{
135+
"_id": ...,
136+
"runtime": 1256,
137+
"title": "Centennial",
138+
...,
139+
},
140+
{
141+
"_id": ...,
142+
"runtime": 1140,
143+
"title": "Baseball",
144+
...,
145+
},
146+
...
147+
]
84148

85149
.. include:: /includes/usage-examples/fact-edit-laravel-app.rst
86-
87-
.. tip::
88-
89-
To learn about other ways to retrieve documents with the {+odm-short+}, see the
90-
:ref:`laravel-fundamentals-retrieve` guide.

0 commit comments

Comments
 (0)