Skip to content

Commit 87ffd83

Browse files
committed
DOCSP-38327: add qb examples to usage exs
1 parent 4af26e7 commit 87ffd83

File tree

8 files changed

+179
-72
lines changed

8 files changed

+179
-72
lines changed

docs/includes/usage-examples/CountTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ public function testCount(): void
2929
],
3030
]);
3131

32-
// begin-count
32+
// begin-eloquent-count
3333
$count = Movie::where('genres', 'Biography')
3434
->count();
3535

3636
echo 'Number of documents: ' . $count;
37-
// end-count
37+
// end-eloquent-count
38+
39+
// begin-qb-count
40+
$count = DB::table('movies')
41+
->where('genres', 'Biography')
42+
->count();
43+
44+
echo 'Number of documents: ' . $count;
45+
// end-qb-count
3846

3947
$this->assertEquals(2, $count);
4048
$this->expectOutputString('Number of documents: 2');

docs/includes/usage-examples/DeleteManyTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ public function testDeleteMany(): void
2929
],
3030
]);
3131

32-
// begin-delete-many
32+
// begin-eloquent-delete-many
3333
$deleted = Movie::where('year', '<=', 1910)
3434
->delete();
3535

3636
echo 'Deleted documents: ' . $deleted;
37-
// end-delete-many
37+
// end-eloquent-delete-many
38+
39+
// begin-qb-delete-many
40+
$deleted = DB::table('movies')
41+
->where('year', '<=', 1910)
42+
->delete();
43+
44+
echo 'Deleted documents: ' . $deleted;
45+
// end-qb-delete-many
3846

3947
$this->assertEquals(2, $deleted);
4048
$this->expectOutputString('Deleted documents: 2');
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
|operator-description| by creating a query builder, using a method such
2-
as ``Model::where()`` or the ``DB`` facade to match documents in a collection, and then calling |result-operation|.
1+
|operator-description| by using a method such as ``Model::where()`` or
2+
methods from the ``DB`` facade to match documents, and then calling
3+
|result-operation|.

docs/usage-examples.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ operations. Each usage example includes the following components:
4343
- Example code that you can run from an application controller
4444
- Output displayed by the print statement
4545

46+
To learn more about the operations demonstrated in the usage examples,
47+
see the :ref:`laravel-fundamentals-read-ops` and
48+
:ref:`laravel-fundamentals-write-ops` guides.
49+
4650
How to Use the Usage Examples
4751
-----------------------------
4852

docs/usage-examples/count.txt

Lines changed: 70 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,79 @@ Count Documents
2525

2626
.. replacement:: result-operation
2727

28-
the ``count()`` method to retrieve the results.
28+
the ``count()`` method to retrieve the results
2929

3030
Example
3131
-------
3232

33-
This usage example performs the following actions:
34-
35-
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
36-
``sample_mflix`` database
37-
- Counts the documents from the ``movies`` collection that match a query filter
38-
- Prints the matching document count
39-
40-
The example calls the following methods on the ``Movie`` model:
41-
42-
- ``where()``: Matches documents in which the value of the ``genres`` field includes ``"Biography"``.
43-
- ``count()``: Counts the number of matching documents. This method returns an integer value.
44-
45-
.. io-code-block::
46-
47-
.. input:: ../includes/usage-examples/CountTest.php
48-
:start-after: begin-count
49-
:end-before: end-count
50-
:language: php
51-
:dedent:
52-
53-
.. output::
54-
:language: console
55-
:visible: false
56-
57-
Number of documents: 1267
33+
Select from the following :guilabel:`Eloquent` and :guilabel:`Query
34+
Builder` tabs to view usage examples for the same operation that use
35+
each corresponding query syntax:
36+
37+
.. tabs::
38+
39+
.. tab:: Eloquent
40+
:tabid: eloquent-model-count
41+
42+
This example performs the following actions:
43+
44+
- Uses the ``Movie`` Eloquent model to represent the ``movies``
45+
collection in the ``sample_mflix`` database
46+
- Counts the documents from the ``movies`` collection that match a
47+
query filter
48+
- Prints the matching document count
49+
50+
The example calls the following methods on the ``Movie`` model:
51+
52+
- ``where()``: Matches documents in which the value of the
53+
``genres`` field includes ``"Biography"``
54+
- ``count()``: Counts the number of matching documents and returns
55+
the count as an integer
56+
57+
.. io-code-block::
58+
59+
.. input:: ../includes/usage-examples/CountTest.php
60+
:start-after: begin-eloquent-count
61+
:end-before: end-eloquent-count
62+
:language: php
63+
:dedent:
64+
65+
.. output::
66+
:language: console
67+
:visible: false
68+
69+
Number of documents: 1267
70+
71+
.. tab:: Query Builder
72+
:tabid: query-builder-count
73+
74+
This example performs the following actions:
75+
76+
- Accesses the ``movies`` collection by calling the ``table()``
77+
method from the ``DB`` facade
78+
- Counts the documents from the ``movies`` collection that match a
79+
query filter
80+
- Prints the matching document count
81+
82+
The example calls the following query builder methods:
83+
84+
- ``where()``: Matches documents in which the value of the
85+
``genres`` field includes ``"Biography"``
86+
- ``count()``: Counts the number of matching documents and returns
87+
the count as an integer
88+
89+
.. io-code-block::
90+
91+
.. input:: ../includes/usage-examples/CountTest.php
92+
:start-after: begin-qb-count
93+
:end-before: end-qb-count
94+
:language: php
95+
:dedent:
96+
97+
.. output::
98+
:language: console
99+
:visible: false
100+
101+
Number of documents: 1267
58102

59103
.. include:: /includes/usage-examples/fact-edit-laravel-app.rst

docs/usage-examples/deleteMany.txt

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,90 @@ Delete Multiple Documents
1717
:depth: 1
1818
:class: singlecol
1919

20-
You can delete multiple documents in a collection by calling the ``delete()`` method on an
21-
object collection or a query builder.
20+
You can delete multiple documents in a collection by calling the
21+
``delete()`` method on an object collection or a query builder.
2222

23-
To delete multiple documents, pass a query filter to the ``where()`` method. Then, delete the
24-
matching documents by calling the ``delete()`` method.
25-
26-
Example
27-
-------
28-
29-
This usage example performs the following actions:
30-
31-
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
32-
``sample_mflix`` database
33-
- Deletes documents from the ``movies`` collection that match a query filter
34-
- Prints the number of deleted documents
35-
36-
The example calls the following methods on the ``Movie`` model:
37-
38-
- ``where()``: matches documents in which the value of the ``year`` field is less than or
39-
equal to ``1910``.
40-
- ``delete()``: deletes the matched documents. This method returns the number
41-
of documents that the method successfully deletes.
42-
43-
.. io-code-block::
44-
:copyable: true
45-
46-
.. input:: ../includes/usage-examples/DeleteManyTest.php
47-
:start-after: begin-delete-many
48-
:end-before: end-delete-many
49-
:language: php
50-
:dedent:
51-
52-
.. output::
53-
:language: console
54-
:visible: false
55-
56-
Deleted documents: 7
57-
58-
.. include:: /includes/usage-examples/fact-edit-laravel-app.rst
23+
To delete multiple documents, pass a query filter to the ``where()``
24+
method. Then, delete the matching documents by calling the ``delete()``
25+
method.
5926

6027
.. tip::
6128

6229
To learn more about deleting documents with the {+odm-short+}, see the :ref:`laravel-fundamentals-delete-documents`
6330
section of the Write Operations guide.
6431

32+
Example
33+
-------
34+
35+
Select from the following :guilabel:`Eloquent` and :guilabel:`Query
36+
Builder` tabs to view usage examples for the same operation that use
37+
each corresponding query syntax:
38+
39+
.. tabs::
40+
41+
.. tab:: Eloquent
42+
:tabid: eloquent-model-count
43+
44+
This example performs the following actions:
45+
46+
- Uses the ``Movie`` Eloquent model to represent the ``movies``
47+
collection in the ``sample_mflix`` database
48+
- Deletes documents from the ``movies`` collection that match a
49+
query filter
50+
- Prints the number of deleted documents
51+
52+
The example calls the following methods on the ``Movie`` model:
53+
54+
- ``where()``: Matches documents in which the value of the
55+
``year`` field is less than or equal to ``1910``
56+
- ``delete()``: Deletes the matched documents and returns the
57+
number of documents successfully deleted
58+
59+
.. io-code-block::
60+
:copyable: true
61+
62+
.. input:: ../includes/usage-examples/DeleteManyTest.php
63+
:start-after: begin-eloquent-delete-many
64+
:end-before: end-eloquent-delete-many
65+
:language: php
66+
:dedent:
67+
68+
.. output::
69+
:language: console
70+
:visible: false
71+
72+
Deleted documents: 7
73+
74+
.. tab:: Query Builder
75+
:tabid: query-builder-count
76+
77+
This example performs the following actions:
78+
79+
- Accesses the ``movies`` collection by calling the ``table()``
80+
method from the ``DB`` facade
81+
- Deletes documents from the ``movies`` collection that match a
82+
query filter
83+
- Prints the number of deleted documents
84+
85+
The example calls the following query builder methods:
86+
87+
- ``where()``: Matches documents in which the value of the
88+
``year`` field is less than or equal to ``1910``
89+
- ``delete()``: Deletes the matched documents and returns the
90+
number of documents successfully deleted
91+
92+
.. io-code-block::
93+
94+
.. input:: ../includes/usage-examples/DeleteManyTest.php
95+
:start-after: begin-qb-delete-many
96+
:end-before: end-qb-delete-many
97+
:language: php
98+
:dedent:
99+
100+
.. output::
101+
:language: console
102+
:visible: false
103+
104+
Deleted documents: 7
105+
106+
.. include:: /includes/usage-examples/fact-edit-laravel-app.rst

docs/usage-examples/find.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Find Multiple Documents
2525

2626
.. replacement:: result-operation
2727

28-
the ``get()`` method to retrieve the results.
28+
the ``get()`` method to retrieve the results
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

docs/usage-examples/findOne.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Find a Document
1919

2020
.. replacement:: result-operation
2121

22-
the ``first()`` method to return one document.
22+
the ``first()`` method to return one document
2323

2424
If multiple documents match the query filter, ``first()`` returns the first matching document according to the documents'
2525
:term:`natural order` in the database or according to the sort order that you can specify

0 commit comments

Comments
 (0)