-
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?
QueryBuilder
multiply and divide support
#3373
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this nice feature that leverage MongoDB update operator.
Could you tell me a bit more about your use-case?
Case of storing price in centsWe need to get the price converted from cents to USD. DB::table('products')->where('title', 'socks')->divide('price', 100); Case of adding taxDB::table('products')->where('title', 'socks')->multiply('price', 1.2); |
* @param float|int $amount | ||
* @return int | ||
*/ | ||
public function multiply($column, $amount = 1, array $extra = [], array $options = []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$amount
should not have a default value. Multiplying by 1 is no-op, unless you have extra values.
$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 comment
The 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 0
.
$this->assertEquals(0, $user->salary); | |
$this->assertNull($user->salary); |
Hello 👋🏻
In this PR I have added
multiply
anddivide
toQueryBuilder
.Usage
Checklist