Skip to content

QueryBuilder multiply and divide support #3373

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

Open
wants to merge 3 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,33 @@ public function decrementEach(array $columns, array $extra = [], array $options
return $this->incrementEach($decrement, $extra, $options);
}

/** @inheritdoc */
public function multiply($column, $amount = 1, array $extra = [], array $options = [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't find this method in the parent class. I only find Illuminate\Collections\Collection::multiply.

Could you remove the @inheritdoc annotation and add a description for all @param and @return.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👌🏻

{
$query = ['$mul' => [(string) $column => $amount]];

if (! empty($extra)) {
$query['$set'] = $extra;
}

// Protect
$this->where(function ($query) use ($column) {
$query->where($column, 'exists', false);

$query->orWhereNotNull($column);
});

$options = $this->inheritConnectionOptions($options);

return $this->performUpdate($query, $options);
}

/** @inheritdoc */
public function divide($column, $amount = 1, array $extra = [], array $options = [])
{
return $this->multiply($column, 1 / $amount, $extra, $options);
}

/** @inheritdoc */
public function pluck($column, $key = null)
{
Expand Down
49 changes: 49 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,55 @@ public function testIncrement()
$this->assertEquals(1, $user->age);
}

public function testMultiplyAndDivide()
{
DB::table('users')->insert([
['name' => 'John Doe', 'salary' => 88000, 'note' => 'senior'],
['name' => 'Jane Doe', 'salary' => 64000, 'note' => 'junior'],
['name' => 'Robert Roe', 'salary' => null],
['name' => 'Mark Moe'],
]);

$user = DB::table('users')->where('name', 'John Doe')->first();
$this->assertEquals(88000, $user->salary);

DB::table('users')->where('name', 'John Doe')->multiply('salary');
$user = DB::table('users')->where('name', 'John Doe')->first();
$this->assertEquals(88000, $user->salary);

DB::table('users')->where('name', 'John Doe')->divide('salary');
$user = DB::table('users')->where('name', 'John Doe')->first();
$this->assertEquals(88000, $user->salary);

DB::table('users')->where('name', 'John Doe')->multiply('salary', 2);
$user = DB::table('users')->where('name', 'John Doe')->first();
$this->assertEquals(176000, $user->salary);

DB::table('users')->where('name', 'John Doe')->divide('salary', 2);
$user = DB::table('users')->where('name', 'John Doe')->first();
$this->assertEquals(88000, $user->salary);

DB::table('users')->where('name', 'Jane Doe')->multiply('salary', 10, ['note' => 'senior']);
$user = DB::table('users')->where('name', 'Jane Doe')->first();
$this->assertEquals(640000, $user->salary);
$this->assertEquals('senior', $user->note);

DB::table('users')->where('name', 'John Doe')->divide('salary', 2, ['note' => 'junior']);
$user = DB::table('users')->where('name', 'John Doe')->first();
$this->assertEquals(44000, $user->salary);
$this->assertEquals('junior', $user->note);

DB::table('users')->multiply('salary');
$user = DB::table('users')->where('name', 'John Doe')->first();
$this->assertEquals(44000, $user->salary);
$user = DB::table('users')->where('name', 'Jane Doe')->first();
$this->assertEquals(640000, $user->salary);
$user = DB::table('users')->where('name', 'Robert Roe')->first();
$this->assertNull($user->salary);
$user = DB::table('users')->where('name', 'Mark Moe')->first();
$this->assertEquals(0, $user->salary);
}

public function testProjections()
{
DB::table('items')->insert([
Expand Down
Loading