Skip to content

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 13 commits into from
Apr 8, 2024
40 changes: 40 additions & 0 deletions docs/includes/usage-examples/DeleteOneTest.php
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');
}
}
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ calls the controller function and returns the result to a web interface.

/usage-examples/findOne
/usage-examples/updateOne
/usage-examples/deleteOne
69 changes: 69 additions & 0 deletions docs/usage-examples/deleteOne.txt
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.