Update table1 field with table2 field value in join laravel fluent

一曲冷凌霜 提交于 2019-12-12 05:48:01

问题


$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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!