如何使用 db::raw 对 Laravel fluent 进行更新查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20113025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do update query on laravel fluent using db::raw
提问by Bryan P
This is related to one of my question earlier where:
这与我之前的一个问题有关:
Update table1 field with table2 field value in join laravel fluent
在join laravel fluent中使用table2字段值更新table1字段
But since this is a different approach now, I will just ask another question:
但由于现在这是一种不同的方法,我将问另一个问题:
How do you properly do an update using DB:raw
?
你如何正确地使用更新DB:raw
?
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.
我想favorite_contents.type
用 contents.type 的值更新,但它没有做任何事情,1 to 的静态设置favorite_contents.expired
正在工作。
This is my code which still doesn't update the type even when the DB::raw
was used:
这是我的代码,即使使用了它仍然不会更新类型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");
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: This is working when done on an sql editor:
PS:这是在 sql 编辑器上完成的工作:
UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id
UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id
回答by AMIB
code raw updates like this:
代码原始更新是这样的:
...->update( array(
'column' => DB::raw( 'column * 2' )
) );
回答by Ironwind
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).
尝试DB::statement
不涉及输出内容的原始查询(选择)。
回答by Ivan Pirus
And will be work such similar, simple realization in Laravel 5.2 , Query Builder:
并将在 Laravel 5.2 Query Builder 中实现类似的简单实现:
DB::table('stores')
->where('id', $request)
->update(['visibility' =>DB::raw($value)]);
This response is tested real site and working properly
此响应经过真实站点测试并正常工作