简体   繁体   English

计数更新查询在Laravel中不起作用

[英]Count in update query doesn't work in Laravel

public function increment($id)
{
    $this->model->where("id",'=', $id)->update(['rating'=> DB::raw('count+1')]);
}

I am getting the following error: 我收到以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in 'field list' (SQL: update news set rating = count+1, updated_at = 2019-04-13 08:12:51 where id = 5) SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'count'(SQL:更新newsrating = count + 1, updated_at = 2019-04-13 08:12:51其中id = 5)

I also tried 我也试过了

->update(['rating'=>'count+1']);

You are not telling the query builder on which table you are performing the query, so DB::raw('count+1') makes no sense. 您没有告诉查询构建器您正在执行查询的表,因此DB::raw('count+1')没有意义。

You can try to use the eloquent increment method like this: 您可以尝试使用如下的雄辩增量方法:

$this->model->where("id", $id)->increment('rating');

Thanks @Tharaka removed the extra call to save(). 谢谢@Tharaka删除了对save()的额外调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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