diff --git a/docs/includes/usage-examples/DeleteOneTest.php b/docs/includes/usage-examples/DeleteOneTest.php new file mode 100644 index 000000000..1a2acd4e0 --- /dev/null +++ b/docs/includes/usage-examples/DeleteOneTest.php @@ -0,0 +1,40 @@ + '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'); + } +} diff --git a/docs/usage-examples.txt b/docs/usage-examples.txt index 2bcd9ac58..32e876fa7 100644 --- a/docs/usage-examples.txt +++ b/docs/usage-examples.txt @@ -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 diff --git a/docs/usage-examples/deleteOne.txt b/docs/usage-examples/deleteOne.txt new file mode 100644 index 000000000..762cfd405 --- /dev/null +++ b/docs/usage-examples/deleteOne.txt @@ -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 `. + +.. tip:: + + To learn more about deleting documents with {+odm-short+}, see the `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. +