laravel 表达式无法转换为数字错误如何解决?

Posted

技术标签:

【中文标题】laravel 表达式无法转换为数字错误如何解决?【英文标题】:laravel Expression could not be converted to number error how to solve? 【发布时间】:2021-07-02 20:42:03 【问题描述】:

我有下面的代码,它将更新表的行以将整数值添加到现有行

       public function updateValue(Request $request)
     
  $pp =
    DB::table('table_tbl')
                             ->update(['value'=> DB::raw('value') +$request->val])
                             ->where('value_id', $request->value_id);
   

错误是Object of class Illuminate\Database\Query\Expression could not be converted to number

【问题讨论】:

这个DB::raw('value') +$request->val 无效... 解决办法是什么? 【参考方案1】:

我还没有对此进行测试,但下面的查询应该可以工作。

// validate val to be integer before using this query
DB::table('table_tbl')
    ->where('value_id', $request->value_id)
    ->update(['value' => DB::raw('value + '.$request->val)]);

【讨论】:

以上是关于laravel 表达式无法转换为数字错误如何解决?的主要内容,如果未能解决你的问题,请参考以下文章