问题
$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"
));
The "$table.expired" => 1
is working fine, but the "$table.type" => "contents.type"
doesn't.
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?
回答1:
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.
来源:https://stackoverflow.com/questions/20111763/update-table1-field-with-table2-field-value-in-join-laravel-fluent