douyin8623
2019-06-27 07:01
浏览 307
已采纳

使用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();
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doumaoao0182 2019-06-27 07:04
    已采纳

    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')]);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • donglang8008 2019-06-27 07:03

    you need to do this

    $flight = App\Flight::find(1);
    
    $flight->value = $flight->value-1;
    
    $flight->save();
    

    hope it helps! :)

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题