Skip to content

Commit 42ceb51

Browse files
committed
DOCSP-50607: multiply/divide QB methods
1 parent b952e76 commit 42ceb51

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

docs/includes/query-builder/QueryBuilderTest.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ public function testGroupBy(): void
213213
{
214214
// begin query groupBy
215215
$result = DB::table('movies')
216-
->where('rated', 'G')
217-
->groupBy('runtime')
218-
->orderBy('runtime', 'asc')
219-
->get(['title']);
216+
->where('rated', 'G')
217+
->groupBy('runtime')
218+
->orderBy('runtime', 'asc')
219+
->get(['title']);
220220
// end query groupBy
221221

222222
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
@@ -420,10 +420,10 @@ public function testWhereRaw(): void
420420
// begin query raw
421421
$result = DB::table('movies')
422422
->whereRaw([
423-
'imdb.votes' => ['$gte' => 1000 ],
423+
'imdb.votes' => ['$gte' => 1000],
424424
'$or' => [
425425
['imdb.rating' => ['$gt' => 7]],
426-
['directors' => ['$in' => [ 'Yasujiro Ozu', 'Sofia Coppola', 'Federico Fellini' ]]],
426+
['directors' => ['$in' => ['Yasujiro Ozu', 'Sofia Coppola', 'Federico Fellini']]],
427427
],
428428
])->get();
429429
// end query raw
@@ -470,7 +470,7 @@ public function testNear(): void
470470
{
471471
$this->importTheaters();
472472

473-
// begin query near
473+
// begin query near
474474
$results = DB::table('theaters')
475475
->where('location.geo', 'near', [
476476
'$geometry' => [
@@ -588,7 +588,7 @@ public function testUpdateUpsert(): void
588588
[
589589
'plot' => 'An autobiographical movie',
590590
'year' => 1998,
591-
'writers' => [ 'Will Hunting' ],
591+
'writers' => ['Will Hunting'],
592592
],
593593
['upsert' => true],
594594
);
@@ -597,6 +597,26 @@ public function testUpdateUpsert(): void
597597
$this->assertIsInt($result);
598598
}
599599

600+
public function testMultiplyDivide(): void
601+
{
602+
// begin multiply divide
603+
$result = DB::table('movies')
604+
->where('year', 2001)
605+
->multiply('imdb.votes', 5)
606+
->divide('runtime', 2);
607+
// end multiply divide
608+
609+
$this->assertIsInt($result);
610+
611+
// begin multiply with set
612+
$result = DB::table('movies')
613+
->where('year', 1958)
614+
->multiply('runtime', 1.5, ['note' => 'Adds recovered footage & interviews.']);
615+
// end multiply with set
616+
617+
$this->assertIsInt($result);
618+
}
619+
600620
public function testIncrement(): void
601621
{
602622
// begin increment

docs/query-builder.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,7 @@ This section includes query builder examples that show how to use the
11691169
following MongoDB-specific write operations:
11701170

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

1256+
.. _laravel-mongodb-query-builder-mul-div:
1257+
1258+
Multiply and Divide Numerical Values Example
1259+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1260+
1261+
You can perform multiplication and division operations on numerical
1262+
values by using the ``multiply()`` and ``divide()`` query builder
1263+
methods.
1264+
1265+
The following example shows how to use the ``multiply()`` and
1266+
``divide()`` query builder methods to manipulate the values of the
1267+
``imdb.votes`` and ``runtime`` fields:
1268+
1269+
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
1270+
:language: php
1271+
:dedent:
1272+
:start-after: begin multiply divide
1273+
:end-before: end multiply divide
1274+
1275+
.. tip:: update() Method
1276+
1277+
You can perform the same operations by using the ``update()``
1278+
method and passing an update document that includes the :manual:`$mul
1279+
</reference/operator/update/mul/>` operator. To learn more about
1280+
``update()``, see the :ref:`laravel-fundamentals-write-modify` guide.
1281+
1282+
You can optionally pass an array parameter to perform a ``$set`` update
1283+
in the same operation, as shown in the following example:
1284+
1285+
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
1286+
:language: php
1287+
:dedent:
1288+
:start-after: begin multiply with set
1289+
:end-before: end multiply with set
1290+
12551291
.. _laravel-mongodb-query-builder-increment:
12561292

12571293
Increment a Numerical Value Example

0 commit comments

Comments
 (0)