-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35976: Delete One usage example #2821
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7092961
DOCSP-35976: Delete One usage example
norareidy e15e104
apply phpcbf formatting
norareidy bbab077
small edit
norareidy 7ff62f8
Merge branch 'DOCSP-35976-delete-one' of github.com:norareidy/laravel…
norareidy cee53ff
JS feedback
norareidy eda988a
JT feedback
norareidy f3f2573
merging
norareidy 671993f
JT feedback 2
norareidy 36bd315
Merge remote-tracking branch 'upstream/4.1' into DOCSP-35976-delete-one
norareidy 2739d2f
assertion edit
norareidy c2f4d6e
edit, code assertion
norareidy f552540
tests
norareidy c3213d4
assertion
norareidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Movie; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class DeleteOneTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testDeleteOne(): void | ||
{ | ||
require_once __DIR__ . '/Movie.php'; | ||
|
||
Movie::truncate(); | ||
Movie::insert([ | ||
[ | ||
'title' => 'Quiz Show', | ||
'runtime' => 133, | ||
], | ||
]); | ||
|
||
// begin-delete-one | ||
$deleted = Movie::where('title', 'Quiz Show') | ||
->orderBy('_id') | ||
->limit(1) | ||
->delete(); | ||
|
||
echo 'Deleted documents: ' . $deleted; | ||
// end-delete-one | ||
|
||
$this->assertEquals(1, $deleted); | ||
$this->expectOutputString('Deleted documents: 1'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
.. _laravel-delete-one-usage: | ||
|
||
================= | ||
Delete a Document | ||
================= | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: delete one, remove, code example | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 1 | ||
:class: singlecol | ||
|
||
You can delete a document in a collection by retrieving a single Eloquent model and calling | ||
the ``delete()`` method, or by calling ``delete()`` directly on a query builder. | ||
|
||
To delete a document, pass a query filter to the ``where()`` method, sort the matching documents, | ||
and call the ``limit()`` method to retrieve only the first document. Then, delete this matching | ||
document by calling the ``delete()`` method. | ||
|
||
Example | ||
------- | ||
|
||
This usage example performs the following actions: | ||
|
||
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the | ||
``sample_mflix`` database | ||
- Deletes a document from the ``movies`` collection that matches a query filter | ||
|
||
The example calls the following methods on the ``Movie`` model: | ||
|
||
- ``where()``: matches documents in which the value of the ``title`` field is ``'Quiz Show'`` | ||
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values | ||
- ``limit()``: retrieves only the first matching document | ||
- ``delete()``: deletes the retrieved document | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: ../includes/usage-examples/DeleteOneTest.php | ||
:start-after: begin-delete-one | ||
:end-before: end-delete-one | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
Deleted documents: 1 | ||
|
||
For instructions on editing your Laravel application to run the usage example, see the | ||
:ref:`Usage Example landing page <laravel-usage-examples>`. | ||
|
||
.. tip:: | ||
|
||
To learn more about deleting documents with {+odm-short+}, see the `Deleting Models | ||
<https://laravel.com/docs/{+laravel-docs-version+}/eloquent#deleting-models>`__ section of the | ||
Laravel documentation. | ||
|
||
For more information about query filters, see the :ref:`laravel-retrieve-matching` section of | ||
the Read Operations guide. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.