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

推荐答案

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

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

Laravel相关问答推荐

在Laravel Livewire中,如果一个输入基于另一个唯一的字段是唯一的,如何判断验证?

Laravel where 子句只返回一个数据库条目

在 laravel 5.3 中截断的错误日志(log)

Laravel Livewire 组件在刷新后不会自动刷新/重新加载

Laravel 5 Eloquent 范围连接并 Select 特定列

安卓 retrofit |发布自定义对象(将 json 发送到服务器)

实施 Payum/Laravel 定期付款

Laravel 中的合同和 PHP 中的接口有什么区别?

使用数据库用户提供程序时如何在 Laravel 中创建密码重置方法

如何在包中安排 Artisan 命令?

Laravel 5:完整性约束违规:1452 无法添加或更新子行:外键约束失败

如何从 laravel 中的现有数据库创建迁移

Laravel 和 PHPStorm 项目的 gitignore 中包含什么?

如果值存在于另一个字段数组中,Laravel 验证规则

约定表名(带下划线)

如何调试 Laravel 框架?

Mac OS X 需要 Mcrypt PHP 扩展

Eloquent 模型不更新 updated_at 时间戳

登录后 DevTools 无法解析 SourceMap 错误将重定向到该 js 文件

如何在 Laravel 中显示动态复选框的旧数据?