This is related to one of my question earlier where:
Update table1 field with table2 field value in join laravel fluent
But since this is a different approach now, I will just ask another question:
How do you properly do an update using DB:raw
?
我想用内容的值更新favorite_contents.type
.输入,但它不做任何事情,静态设置1到favorite_contents.expired
正在工作.
这是我的代码,即使使用了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: This is working when done on an sql editor:
UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id