如何使用db :: raw在laravel fluent上更新查询 [英] How to do update query on laravel fluent using db::raw

查看:158
本文介绍了如何使用db :: raw在laravel fluent上更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我先前的问题之一有关:

This is related to one of my question earlier where:

使用table2字段值更新table1字段加入laravel流利

但是由于现在这是一种不同的方法,所以我将要问另一个问题:

But since this is a different approach now, I will just ask another question:

如何使用DB:raw正确执行更新?

How do you properly do an update using DB:raw?

我想用contents.type的值更新favorite_contents.type,但是它什么也没做,favorite_contents.expired的静态设置为1.

I want to update the favorite_contents.type with the value of contents.type, but it doesn't do anything, the static setting of 1 to favorite_contents.expired is working.

这是我的代码,即使使用DB::raw,它也不会更新类型:

This is my code which still doesn't update the type even when the DB::raw was used:

$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");

这是在我使用上面的代码也无法正常工作之前不会更新的第一个代码:

This is the first code that doesn't update before I resorted to the above code that doesn't work as well:

$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编辑器上完成此操作后即可使用

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

推荐答案

DB::statement("UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id");

对于不涉及输出内容(选择)的原始查询,请尝试DB::statement.

Try DB::statement for raw queries that does not involve outputting something (select).

这篇关于如何使用db :: raw在laravel fluent上更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆