简体   繁体   中英

directly increment or decrement number update from column using Laravel Eloquent

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();

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')]);

you need to do this

$flight = App\Flight::find(1);

$flight->value = $flight->value-1;

$flight->save();

hope it helps! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM