Skip to content

DOCSP-50607: multiply/divide QB methods #3403

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 4 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/eloquent-models/schema-builder.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ in the Laravel documentation.
Implement Schema Validation
---------------------------

You can use the ``jsonSchema()`` method to implement :manual:`schema
validation </core/schema-validation/>` when using the following schema
builder methods:
Starting in {+odm-short+} v5.5, you can use the ``jsonSchema()`` method
to implement :manual:`schema validation </core/schema-validation/>` when
using the following schema builder methods:

- ``Schema::create()``: When creating a new collection
- ``Schema::table()``: When updating collection properties
Expand Down
39 changes: 31 additions & 8 deletions docs/includes/query-builder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ public function testGroupBy(): void
{
// begin query groupBy
$result = DB::table('movies')
->where('rated', 'G')
->groupBy('runtime')
->orderBy('runtime', 'asc')
->get(['title']);
->where('rated', 'G')
->groupBy('runtime')
->orderBy('runtime', 'asc')
->get(['title']);
// end query groupBy

$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
Expand Down Expand Up @@ -420,10 +420,10 @@ public function testWhereRaw(): void
// begin query raw
$result = DB::table('movies')
->whereRaw([
'imdb.votes' => ['$gte' => 1000 ],
'imdb.votes' => ['$gte' => 1000],
'$or' => [
['imdb.rating' => ['$gt' => 7]],
['directors' => ['$in' => [ 'Yasujiro Ozu', 'Sofia Coppola', 'Federico Fellini' ]]],
['directors' => ['$in' => ['Yasujiro Ozu', 'Sofia Coppola', 'Federico Fellini']]],
],
])->get();
// end query raw
Expand Down Expand Up @@ -470,7 +470,7 @@ public function testNear(): void
{
$this->importTheaters();

// begin query near
// begin query near
$results = DB::table('theaters')
->where('location.geo', 'near', [
'$geometry' => [
Expand Down Expand Up @@ -588,7 +588,7 @@ public function testUpdateUpsert(): void
[
'plot' => 'An autobiographical movie',
'year' => 1998,
'writers' => [ 'Will Hunting' ],
'writers' => ['Will Hunting'],
],
['upsert' => true],
);
Expand All @@ -597,6 +597,29 @@ public function testUpdateUpsert(): void
$this->assertIsInt($result);
}

public function testMultiplyDivide(): void
{
// begin multiply divide
$result = DB::table('movies')
->where('year', 2001)
->multiply('imdb.votes', 5);

$result = DB::table('movies')
->where('year', 2001)
->divide('runtime', 2);
// end multiply divide

$this->assertIsInt($result);

// begin multiply with set
$result = DB::table('movies')
->where('year', 1958)
->multiply('runtime', 1.5, ['note' => 'Adds recovered footage.']);
// end multiply with set

$this->assertIsInt($result);
}

public function testIncrement(): void
{
// begin increment
Expand Down
36 changes: 36 additions & 0 deletions docs/query-builder.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,7 @@ This section includes query builder examples that show how to use the
following MongoDB-specific write operations:

- :ref:`Upsert a document <laravel-mongodb-query-builder-upsert>`
- :ref:`Multiply and divide values <laravel-mongodb-query-builder-mul-div>`
- :ref:`Increment a numerical value <laravel-mongodb-query-builder-increment>`
- :ref:`Decrement a numerical value <laravel-mongodb-query-builder-decrement>`
- :ref:`Add an array element <laravel-mongodb-query-builder-push>`
Expand Down Expand Up @@ -1252,6 +1253,41 @@ and the ``title`` field and value specified in the ``where()`` query operation:
The ``update()`` query builder method returns the number of documents that the
operation updated or inserted.

.. _laravel-mongodb-query-builder-mul-div:

Multiply and Divide Numerical Values Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Starting in {+odm-short+} v5.5, you can perform multiplication and
division operations on numerical values by using the ``multiply()`` and
``divide()`` query builder methods.

The following example shows how to use the ``multiply()`` and
``divide()`` methods to manipulate the values of the
``imdb.votes`` and ``runtime`` fields:

.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
:language: php
:dedent:
:start-after: begin multiply divide
:end-before: end multiply divide

.. tip:: update() Method

You can perform the same operations by using the ``update()``
method and passing an update document that includes the :manual:`$mul
</reference/operator/update/mul/>` operator. To learn more about
``update()``, see the :ref:`laravel-fundamentals-write-modify` guide.

You can optionally pass an array parameter to perform a ``$set`` update
in the same operation, as shown in the following example:

.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
:language: php
:dedent:
:start-after: begin multiply with set
:end-before: end multiply with set

.. _laravel-mongodb-query-builder-increment:

Increment a Numerical Value Example
Expand Down
Loading