这与我之前的一个问题有关,链接如下:
Update table1 field with table2 field value in join laravel fluent
但是由于现在采用了不同的方法,所以我将提出另一个问题:
如何正确地使用DB: raw
进行更新?
我想要将 favorite_contents.type
更新为 contents.type
的值,但它没有做任何事情,而将 favorite_contents.expired
设置为 1 是有效的。
这是我的代码,即使使用了DB::raw
,仍然无法更新类型:
$table = 'favorite_contents';
$contents = DB::table($table)
->join('contents', function($join) use($table){
$join->on("$table.content_id", '=', 'contents.id');
})
->whereIn("$table.content_id",$ids)
->update(array(
"$table.expired" => 1
));
DB::raw("UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id");
这是我尝试了上面那个不起作用的代码之前,第一个没有更新的代码:
$table = 'favorite_contents';
$contents = DB::table($table)
->join('contents', function($join) use($table){
$join->on("$table.content_id", '=', 'contents.id');
})
->whereIn("$table.content_id",$ids)
->update(array(
"$table.expired" => 1,
"$table.type" => "contents.type"
));
P.S: 在 SQL 编辑器中执行以下命令可以正常工作:
UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id