From 125041fa15ae4ede1783febf41a978fa288b65e0 Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 12 Apr 2024 11:33:16 -0400 Subject: [PATCH 1/4] DOCSP-35982: Count usage example --- docs/includes/usage-examples/CountTest.php | 42 ++++++++++++++++ docs/usage-examples.txt | 1 + docs/usage-examples/count.txt | 57 ++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 docs/includes/usage-examples/CountTest.php create mode 100644 docs/usage-examples/count.txt diff --git a/docs/includes/usage-examples/CountTest.php b/docs/includes/usage-examples/CountTest.php new file mode 100644 index 000000000..e7a5e8fe8 --- /dev/null +++ b/docs/includes/usage-examples/CountTest.php @@ -0,0 +1,42 @@ + 'Young Mr. Lincoln', + 'genres' => ['Biography', 'Drama'], + ], + [ + 'title' => 'Million Dollar Mermaid', + 'genres' => ['Biography', 'Drama', 'Musical'], + ], + ]); + + // begin-count + $biographies = Movie::where('genres', 'Biography') + ->count(); + + echo 'Number of documents: ' . $biographies; + // end-count + + $this->assertEquals(2, $biographies); + $this->expectOutputString('Number of documents: 2'); + } +} \ No newline at end of file diff --git a/docs/usage-examples.txt b/docs/usage-examples.txt index 32e876fa7..fe49723d5 100644 --- a/docs/usage-examples.txt +++ b/docs/usage-examples.txt @@ -74,3 +74,4 @@ calls the controller function and returns the result to a web interface. /usage-examples/findOne /usage-examples/updateOne /usage-examples/deleteOne + /usage-examples/count diff --git a/docs/usage-examples/count.txt b/docs/usage-examples/count.txt new file mode 100644 index 000000000..7c5621d7a --- /dev/null +++ b/docs/usage-examples/count.txt @@ -0,0 +1,57 @@ +.. _laravel-count-usage: + +=============== +Count Documents +=============== + +.. facet:: + :name: genre + :values: reference + +.. meta:: + :keywords: total, code example + +.. contents:: On this page + :local: + :backlinks: none + :depth: 1 + :class: singlecol + +You can count the number of documents returned by a query by calling the ``where()`` and +``count()`` methods on an object collection or a query builder. + +To return the number of documents that match a filter, pass the query filter to the ``where()`` +method and call the ``count()`` method. + +Example +------- + +This usage example performs the following actions: + +- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the + ``sample_mflix`` database +- Counts the documents from the ``movies`` collection that match a query filter +- Prints the matching document count + +The example calls the following methods on the ``Movie`` model: + +- ``where()``: matches documents in which the value of the ``genres`` field includes ``"Biography"``. +- ``count()``: counts the number of matching documents. This method returns an integer value. + +.. io-code-block:: + + .. input:: ../includes/usage-examples/CountTest.php + :start-after: begin-count + :end-before: end-count + :language: php + :dedent: + + .. output:: + :language: console + :visible: false + + Matching documents: 1267 + + +To learn how to edit your Laravel application to run the usage example, see the +:ref:`Usage Examples landing page `. From ceddbdaa7308d87537149a71a33852c782deda3c Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 12 Apr 2024 15:35:16 +0000 Subject: [PATCH 2/4] apply phpcbf formatting --- docs/includes/usage-examples/CountTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/includes/usage-examples/CountTest.php b/docs/includes/usage-examples/CountTest.php index e7a5e8fe8..930d8c185 100644 --- a/docs/includes/usage-examples/CountTest.php +++ b/docs/includes/usage-examples/CountTest.php @@ -39,4 +39,4 @@ public function testCount(): void $this->assertEquals(2, $biographies); $this->expectOutputString('Number of documents: 2'); } -} \ No newline at end of file +} From 4707b1a73dd0e637dec8c71f595dbd06506de31b Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 12 Apr 2024 15:16:55 -0400 Subject: [PATCH 3/4] MW feedback --- docs/usage-examples/count.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/usage-examples/count.txt b/docs/usage-examples/count.txt index 7c5621d7a..4a526bf62 100644 --- a/docs/usage-examples/count.txt +++ b/docs/usage-examples/count.txt @@ -35,8 +35,8 @@ This usage example performs the following actions: The example calls the following methods on the ``Movie`` model: -- ``where()``: matches documents in which the value of the ``genres`` field includes ``"Biography"``. -- ``count()``: counts the number of matching documents. This method returns an integer value. +- ``where()``: Matches documents in which the value of the ``genres`` field includes ``"Biography"``. +- ``count()``: Counts the number of matching documents. This method returns an integer value. .. io-code-block:: From 46746ad1029de986d3975950b0827763111bd878 Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 12 Apr 2024 15:33:11 -0400 Subject: [PATCH 4/4] JT feedback --- docs/includes/usage-examples/CountTest.php | 6 +++--- docs/usage-examples/count.txt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/includes/usage-examples/CountTest.php b/docs/includes/usage-examples/CountTest.php index 930d8c185..ecf53db47 100644 --- a/docs/includes/usage-examples/CountTest.php +++ b/docs/includes/usage-examples/CountTest.php @@ -30,13 +30,13 @@ public function testCount(): void ]); // begin-count - $biographies = Movie::where('genres', 'Biography') + $count = Movie::where('genres', 'Biography') ->count(); - echo 'Number of documents: ' . $biographies; + echo 'Number of documents: ' . $count; // end-count - $this->assertEquals(2, $biographies); + $this->assertEquals(2, $count); $this->expectOutputString('Number of documents: 2'); } } diff --git a/docs/usage-examples/count.txt b/docs/usage-examples/count.txt index 4a526bf62..dc3720fc0 100644 --- a/docs/usage-examples/count.txt +++ b/docs/usage-examples/count.txt @@ -18,7 +18,7 @@ Count Documents :class: singlecol You can count the number of documents returned by a query by calling the ``where()`` and -``count()`` methods on an object collection or a query builder. +``count()`` methods on a collection of models or a query builder. To return the number of documents that match a filter, pass the query filter to the ``where()`` method and call the ``count()`` method.