Reputation: 3002
I have a query where in I use Eloquent for finding the ID but what I need is to directly subtract inside the eloquent query ? same as in Query Builder,
Documentation code
$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save();
What I need is to directly to substract
$flight = App\Flight::find(1);
$flight->value =- 1;
$flight->save();
Upvotes: 5
Views: 9289
Reputation: 15115
use laravel increment()
or decrement()
method see
App\Flight::find(1)->decrement('value',1);
second way u can achieve by update
query
App\Flight::where('id', 1)->update(['value' => \DB::raw('value - 1')]);
Upvotes: 8
Reputation: 892
you need to do this
$flight = App\Flight::find(1);
$flight->value = $flight->value-1;
$flight->save();
hope it helps! :)
Upvotes: 1