简体   繁体   English

在 Laravel 中使用 Eloquent 更新记录时如何使用原始 SET 语句?

[英]How to use raw SET statement when updating records with Eloquent in Laravel?

Is it possible to use Eloquent to turn the following SQL statement into PHP codes in Laravel?在 Laravel 中是否可以使用 Eloquent 将以下 SQL 语句转换为 PHP 代码?

UPDATE my_table SET column1 = price*qty*?, column2 = 0 WHERE row_id = ?

If it's not possible with Eloquent (I couldn't find it in Laravel's documentation), using query builder would be acceptable too.如果 Eloquent 无法实现(我在 Laravel 的文档中找不到),那么使用查询构建器也是可以接受的。

Try this:尝试这个:

$item = Item::where('row_id', $row_id)->first();
$item->update(['column1' => $item->price * $item->qty, 'column2' => 0]);

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

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