在联接laravel中使用表2字段值更新表1字段 [英] Update table1 field with table2 field value in join laravel fluent
问题描述
$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"
));
"$table.expired" => 1
可以正常工作,但"$table.type" => "contents.type"
不能正常工作.
The "$table.expired" => 1
is working fine, but the "$table.type" => "contents.type"
doesn't.
因此问题与在内容表中获取type的值有关,如何在不使用foreach的情况下做到这一点?
So the problem has something to do with getting the value of type in the contents table, how do I do this without resorting to foreach?
推荐答案
将"$table.type" => "contents.type"
更改为"$table.type" => DB::raw("contents.type")
.有了它,我相信update()
方法正在尝试保存字符串"contents.type",而不是从"contents"中获取"type"列.如果您使用的是MySQL,并且favourite_contents.type是数字列,则它可能会将"contents.type"转换为0,而不会显示错误.
Change "$table.type" => "contents.type"
to "$table.type" => DB::raw("contents.type")
. As you have it, I believe the update()
method is trying to save the string "contents.type" rather than getting the "type" column from "contents". If you're using MySQL and the favorite_contents.type is numeric column, it might convert "contents.type" to 0 without showing an error.
这篇关于在联接laravel中使用表2字段值更新表1字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!