-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: 5.x
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done on 87b0d48 |
||
{ | ||
$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) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the field doesn't exist, this operation should not create it as it will always be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done on 60c6487 |
||||||
} | ||||||
|
||||||
public function testProjections() | ||||||
{ | ||||||
DB::table('items')->insert([ | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.