Skip to content

Commit 5df1bed

Browse files
Merge 4.2 into 4.3 (#2861)
2 parents a9413a1 + 9a16fce commit 5df1bed

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 DeleteManyTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
* @preserveGlobalState disabled
15+
*/
16+
public function testDeleteMany(): void
17+
{
18+
require_once __DIR__ . '/Movie.php';
19+
20+
Movie::truncate();
21+
Movie::insert([
22+
[
23+
'title' => 'Train Pulling into a Station',
24+
'year' => 1896,
25+
],
26+
[
27+
'title' => 'The Ball Game',
28+
'year' => 1898,
29+
],
30+
]);
31+
32+
// begin-delete-many
33+
$deleted = Movie::where('year', '<=', 1910)
34+
->delete();
35+
36+
echo 'Deleted documents: ' . $deleted;
37+
// end-delete-many
38+
39+
$this->assertEquals(2, $deleted);
40+
$this->expectOutputString('Deleted documents: 2');
41+
}
42+
}

docs/usage-examples.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,4 @@ calls the controller function and returns the result to a web interface.
7474
/usage-examples/findOne
7575
/usage-examples/updateOne
7676
/usage-examples/deleteOne
77+
/usage-examples/deleteMany

docs/usage-examples/deleteMany.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
.. _laravel-delete-many-usage:
2+
3+
=========================
4+
Delete Multiple Documents
5+
=========================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: delete many, remove, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 1
18+
:class: singlecol
19+
20+
You can delete multiple documents in a collection by calling the ``delete()`` method on an
21+
object collection or a query builder.
22+
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 retrieved documents. This method returns the number of documents
41+
that were successfully deleted.
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+
To learn how to edit your Laravel application to run the usage example, see the
59+
:ref:`Usage Examples landing page <laravel-usage-examples>`.
60+
61+
.. tip::
62+
63+
To learn more about deleting documents with {+odm-short+}, see the :ref:`laravel-fundamentals-delete-documents`
64+
section of the Write Operations guide.
65+
66+
For more information about query filters, see the :ref:`laravel-retrieve-matching` section of
67+
the Read Operations guide.
68+

0 commit comments

Comments
 (0)