Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 456326b

Browse files
DOCSP-50607: multiply/divide QB methods (#3403)
* DOCSP-50607: multiply/divide QB methods * separate mul & div examples * add versioning * wip
1 parent b952e76 commit 456326b

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

‎docs/eloquent-models/schema-builder.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ in the Laravel documentation.
117117
Implement Schema Validation
118118
---------------------------
119119

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

124124
- ``Schema::create()``: When creating a new collection
125125
- ``Schema::table()``: When updating collection properties

‎docs/includes/query-builder/QueryBuilderTest.php

Lines changed: 31 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,29 @@ 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+
607+
$result = DB::table('movies')
608+
->where('year', 2001)
609+
->divide('runtime', 2);
610+
// end multiply divide
611+
612+
$this->assertIsInt($result);
613+
614+
// begin multiply with set
615+
$result = DB::table('movies')
616+
->where('year', 1958)
617+
->multiply('runtime', 1.5, ['note' => 'Adds recovered footage.']);
618+
// end multiply with set
619+
620+
$this->assertIsInt($result);
621+
}
622+
600623
public function testIncrement(): void
601624
{
602625
// 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+
Starting in {+odm-short+} v5.5, you can perform multiplication and
1262+
division operations on numerical values by using the ``multiply()`` and
1263+
``divide()`` query builder methods.
1264+
1265+
The following example shows how to use the ``multiply()`` and
1266+
``divide()`` 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)

AltStyle によって変換されたページ (->オリジナル) /