Laravel雄辩的增量计算了更新时的百分比



我有一个整数列,需要用他的百分比增加所选行的值。

我正在尝试这样做:

$value = $request->percentage / 100;
$lineaCalendario  = Calendar::where('rate_id', $idRate)
->whereIn('date', $dates) 
->increment('price', DB::raw('price')*$value); 

这行不通。

不支持的操作数类型:浮点*Illuminate\Database\Query\Expression

但我找不到怎么做。

您可以尝试使用update方法而不是increment,并直接在DB::raw((查询中对价格*$value进行乘法运算。事实上,你不能用一个表达式来乘以你的价值:

Calendar::query()
->where('rate_id', $idRate)
->whereIn('date', $dates)
->update([
'price' => DB::raw("price + (price * ${value})")
])

注意:如Laravel的官方文件所述:

Raw statements will be injected into the query as strings, 
so you should be extremely careful to avoid creating SQL injection vulnerabilities.

最新更新