-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35951: write operations delete docs #2829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 39 commits
8fbbcc4
a5a75ca
a678179
eea96e5
88024a5
24fdd13
77bdbec
831f06e
d813f23
a194e60
2582bd5
86ff663
4dc106a
678490e
8ff6872
44eb655
60a6cfb
a9d072c
79037d2
78bc0e7
1d77153
39eb362
dfe2019
ebf1cc5
2c339bd
f2b0412
cd7f08a
9ba28be
07530e6
e6c162f
4aa7e5c
bb3439b
25a3912
868d3a6
aee0efe
2512f78
78df112
a69a0e7
022622f
1018c36
af90cb7
8830948
d07cb68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,7 +9,7 @@ Write Operations | |||||||||
:values: tutorial | ||||||||||
|
||||||||||
.. meta:: | ||||||||||
:keywords: insert, insert one, update, update one, upsert, code example, mass assignment, push, pull, eloquent model | ||||||||||
:keywords: insert, insert one, update, update one, upsert, code example, mass assignment, push, pull, delete, delete many, primary key, destroy, eloquent model | ||||||||||
|
||||||||||
.. contents:: On this page | ||||||||||
:local: | ||||||||||
|
@@ -28,7 +28,8 @@ This guide shows you how to perform the following tasks: | |||||||||
|
||||||||||
- :ref:`laravel-fundamentals-insert-documents` | ||||||||||
- :ref:`laravel-fundamentals-modify-documents` | ||||||||||
- Delete Documents | ||||||||||
- :ref:`laravel-fundamentals-delete-documents` | ||||||||||
|
||||||||||
|
||||||||||
.. _laravel-fundamentals-write-sample-model: | ||||||||||
|
||||||||||
|
@@ -98,6 +99,7 @@ This example code performs the following actions: | |||||||||
:dedent: | ||||||||||
:start-after: begin model insert one | ||||||||||
:end-before: end model insert one | ||||||||||
:caption: Insert a document by calling the save() method on an instance. | ||||||||||
|
||||||||||
You can retrieve the inserted document's ``_id`` value by accessing the model's | ||||||||||
``id`` member, as shown in the following code example: | ||||||||||
|
@@ -189,6 +191,7 @@ of the model and calling its ``save()`` method: | |||||||||
:dedent: | ||||||||||
:start-after: begin model update one save | ||||||||||
:end-before: end model update one save | ||||||||||
:caption: Update a document by calling the save() method on an instance. | ||||||||||
|
||||||||||
When the ``save()`` method succeeds, the model instance on which you called the | ||||||||||
method contains the updated values. | ||||||||||
|
@@ -203,13 +206,9 @@ retrieve and update the first matching document: | |||||||||
:dedent: | ||||||||||
:start-after: begin model update one fluent | ||||||||||
:end-before: end model update one fluent | ||||||||||
:caption: Update the matching document by chaining the update() method. | ||||||||||
|
||||||||||
.. note:: | ||||||||||
|
||||||||||
The ``orderBy()`` call sorts the results by the ``_id`` field to | ||||||||||
guarantee a consistent sort order. To learn more about sorting in MongoDB, | ||||||||||
see the :manual:`Natural order </reference/glossary/#std-term-natural-order>` | ||||||||||
glossary entry in the {+server-docs-name+}. | ||||||||||
.. include:: /includes/fact-orderby-id.rst | ||||||||||
|
||||||||||
When the ``update()`` method succeeds, the operation returns the number of | ||||||||||
documents updated. | ||||||||||
|
@@ -480,3 +479,133 @@ with ``"contemporary"`` in the ``genres`` array field. Click the | |||||||||
|
||||||||||
To learn more about array update operators, see :manual:`Array Update Operators </reference/operator/update-array/>` | ||||||||||
in the {+server-docs-name+}. | ||||||||||
|
||||||||||
.. _laravel-fundamentals-delete-documents: | ||||||||||
|
||||||||||
Delete Documents | ||||||||||
---------------- | ||||||||||
|
||||||||||
In this section, you can learn how to delete documents from a MongoDB collection | ||||||||||
by using {+odm-short+}. Use delete operations to remove data from your MongoDB | ||||||||||
database permanently. | ||||||||||
|
||||||||||
This section provides examples of the following delete operations: | ||||||||||
|
||||||||||
- :ref:`Delete one document <laravel-fundamentals-delete-one>` | ||||||||||
- :ref:`Delete multiple documents <laravel-fundamentals-delete-many>` | ||||||||||
|
||||||||||
To learn about the Laravel features available in {+odm-short+} that modify | ||||||||||
delete behavior, see the following sections: | ||||||||||
|
||||||||||
- :ref:`Soft delete <laravel-model-soft-delete>`, which lets you mark | ||||||||||
documents as deleted instead of removing them from the database | ||||||||||
- :ref:`Pruning <laravel-model-pruning>`, which lets you define conditions | ||||||||||
that qualify a document for automatic deletion | ||||||||||
|
||||||||||
.. _laravel-fundamentals-delete-one: | ||||||||||
|
||||||||||
Delete a Document Examples | ||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
You can delete one document in the following ways: | ||||||||||
|
||||||||||
- Call the ``destroy()`` method on the model and pass it a primary key to | ||||||||||
delete the matching document. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: revise to avoid the phrasing "pass it"
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed on Slack. Replacing with a adverbial phrase to describe the first part of the sentence. |
||||||||||
- Call the ``delete()`` method on an instance of the model. | ||||||||||
- Chain methods to retrieve and delete an instance of a model by calling the | ||||||||||
``delete()`` method. | ||||||||||
|
||||||||||
The following example shows how to delete a document by passing the value of | ||||||||||
its primary key to the ``destroy()`` method: | ||||||||||
|
||||||||||
.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
:language: php | ||||||||||
:dedent: | ||||||||||
:start-after: begin model delete by id | ||||||||||
:end-before: end model delete by id | ||||||||||
:caption: Delete the document by its primary key. | ||||||||||
|
||||||||||
When the ``destroy()`` method succeeds, it returns the number of documents | ||||||||||
deleted. | ||||||||||
|
||||||||||
If the primary key value does not match any documents, the ``destroy()`` method | ||||||||||
returns returns ``0``. | ||||||||||
|
||||||||||
The following example shows how to delete a document by calling ``delete()`` | ||||||||||
on an instance of the model: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: separate the three examples into subsections or move into bullet points, but as of now stacking them is a bit confusing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with this and had considered earlier on, but wanted to keep all examples at the same heading level. Both Insert and Update use the same formatting with the same intention. I think once these sections (Insert, Modify, Delete) are split into individual pages similar to drivers docs are separate pages, these can be their own sections. In the meantime, I'll add literalinclude captions (including examples in the prior sections) to identify which method is being demonstrated. |
||||||||||
|
||||||||||
.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
:language: php | ||||||||||
:dedent: | ||||||||||
:start-after: begin delete one model | ||||||||||
:end-before: end delete one model | ||||||||||
:caption: Delete the document by calling the delete() method on an instance. | ||||||||||
|
||||||||||
When the ``delete()`` method succeeds, the operation returns the number of | ||||||||||
documents deleted. | ||||||||||
|
||||||||||
If the retrieve part of the call does not match any documents in the collection, | ||||||||||
the operation returns ``0``. | ||||||||||
|
||||||||||
The following example shows how to chain calls to retrieve the first | ||||||||||
matching document and delete it: | ||||||||||
|
||||||||||
.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
:language: php | ||||||||||
:dedent: | ||||||||||
:start-after: begin model delete one fluent | ||||||||||
:end-before: end model delete one fluent | ||||||||||
:caption: Delete the matching document by chaining the delete() method. | ||||||||||
|
||||||||||
.. include:: /includes/fact-orderby-id.rst | ||||||||||
|
||||||||||
When the ``delete()`` method succeeds, it returns the number of documents | ||||||||||
deleted. | ||||||||||
|
||||||||||
If the ``where()`` method does not match any documents, the ``delete()`` method | ||||||||||
returns returns ``0``. | ||||||||||
|
||||||||||
.. _laravel-fundamentals-delete-many: | ||||||||||
|
||||||||||
Delete Multiple Documents Examples | ||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
You can delete multiple documents in the following ways: | ||||||||||
|
||||||||||
- Call the ``destroy()`` method on the model and pass it multiple primary | ||||||||||
keys to delete the matching documents. | ||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
- Chain methods to retrieve a Laravel collection object that references | ||||||||||
multiple objects and delete them by calling the ``delete()`` method. | ||||||||||
|
||||||||||
The following example shows how to delete a document by passing an array of | ||||||||||
primary key values, represented by ``$ids``, to the ``destroy()`` method: | ||||||||||
|
||||||||||
.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
:language: php | ||||||||||
:dedent: | ||||||||||
:start-after: begin model delete multiple by id | ||||||||||
:end-before: end model delete multiple by id | ||||||||||
:caption: Delete documents by their primary keys. | ||||||||||
|
||||||||||
When the ``destroy()`` method succeeds, it returns the number of documents | ||||||||||
deleted. | ||||||||||
|
||||||||||
If the primary key values do not match any documents, the ``destroy()`` method | ||||||||||
returns returns ``0``. | ||||||||||
|
||||||||||
The following example shows how to chain calls to retrieve matching documents | ||||||||||
and delete them: | ||||||||||
|
||||||||||
.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
:language: php | ||||||||||
:dedent: | ||||||||||
:start-after: begin model delete multiple fluent | ||||||||||
:end-before: end model delete multiple fluent | ||||||||||
:caption: Chain calls to retrieve matching documents and delete them. | ||||||||||
|
||||||||||
When the ``delete()`` method succeeds, it returns the number of documents | ||||||||||
deleted. | ||||||||||
|
||||||||||
If the ``where()`` method does not match any documents, the ``delete()`` method | ||||||||||
returns ``0``. | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.. note:: | ||
|
||
The ``orderBy()`` call sorts the results by the ``_id`` field to | ||
guarantee a consistent sort order. To learn more about sorting in MongoDB, | ||
see the :manual:`Natural order </reference/glossary/#std-term-natural-order>` | ||
glossary entry in the {+server-docs-name+}. |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use MongoDB\BSON\UTCDateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use MongoDB\Laravel\Tests\TestCase; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use function array_push; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use function count; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use function in_array; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -329,4 +330,188 @@ public function testModelPositional(): void | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertContains('contemporary', $result->genres); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertFalse(in_array('dance-pop', $result->genres)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteById(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => ['classical'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 2121, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performanceDate' => new UTCDateTime(Carbon::create(2024, 4, 1, 20, 0, 0, 'EST')), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Brad Mehldau', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Philharmonie de Paris', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'jazz', 'post-bop' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 5745, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Billy Joel', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Madison Square Garden', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'rock', 'soft rock', 'pop rock' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 12852, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'The Rolling Stones', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Soldier Field', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'rock', 'pop', 'blues' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 59527, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performanceDate' => new UTCDateTime(Carbon::create(2024, 6, 30, 20, 0, 0, 'CDT')), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(4, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$id = Concert::first()->id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin model delete by id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::destroy($id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end model delete by id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(3, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteModel(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin delete one model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$concert = Concert::first(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$concert->delete(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end delete one model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(0, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteFirst(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Brad Mehldau', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Philharmonie de Paris', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Billy Joel', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Madison Square Garden', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'The Rolling Stones', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Soldier Field', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin model delete one fluent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::where('venue', 'Carnegie Hall') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
->orderBy('_id') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get the point of ordering for deleting. In fact, it's ignored: laravel-mongodb/src/Query/Builder.php Lines 787 to 814 in 95c5b4f
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, so if the where() query filter matched multiple documents, wouldn't it be important to specify the sort order such that it's known which one will surface when calling limit(1), since that's the one that will be deleted? Does that mean that you can't use indexes with sort orders for the query filter part of the delete operation? If not possible, I should probably include that information. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's currently not possible. That could be a new feature PHPORM-169 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: from the server's perspective, there is no way to specify an order with a delete operation. Indexes can certainly be used (and hinted). In the case of a sparse index that might actually have an impact on which document(s) get selected for the deletion. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
->limit(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
->delete(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end model delete one fluent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(3, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteMultipleById(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Brad Mehldau', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Philharmonie de Paris', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Billy Joel', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Madison Square Garden', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'The Rolling Stones', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Soldier Field', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$concerts = Concert::all(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$ids = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foreach ($concerts as $concert) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
array_push($ids, $concert->id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin model delete multiple by id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::destroy($ids); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end model delete multiple by id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(0, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteMultiple(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => ['classical'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 2121, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Brad Mehldau', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Philharmonie de Paris', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'jazz', 'post-bop' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 5745, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Billy Joel', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Madison Square Garden', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'rock', 'soft rock', 'pop rock' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 12852, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'The Rolling Stones', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Soldier Field', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'rock', 'pop', 'blues' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 59527, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin model delete multiple fluent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::where('ticketsSold', '>', 7500) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
->delete(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end model delete multiple fluent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(2, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"permanently" is wrong when the
SoftDeletes
trait is used. But you mention it just below, so maybe that's okay.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for providing that perspective, I'll update the text to make that more clear!