php DB::update laravel 5 原始查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44559252/
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
DB::update laravel 5 raw query
提问by JohnnyCc
I intend to use laravel db update which equivalent to sql.
我打算使用相当于sql的laravel db update。
update users set username = "admin", status = "active" where user_id = 1
This is the query I test to run. Any wrong?
这是我测试运行的查询。有什么不对吗?
$username = "admin";
$status = "active";
DB::update('update users set username = ' .$username. ',
status = '.$status.' where user_id = ?' ,['1']);
回答by AddWeb Solution Pvt Ltd
You should update your query like :
您应该更新您的查询,如:
Eloquent Query:
雄辩的查询:
User::where('user_id',1)->update(array(
'username'=>$username,
));
Fluent Query:
流畅查询:
DB::table('users')->where('user_id',1)->update(array(
'username'=>$username,
));
Hope this helps you
希望这对你有帮助
回答by Anwar Khan
The correct query would be
正确的查询是
DB::update('update users set username = ? , status = ? where user_id = ?', ["admin" , "active" , 1]);
OR
或者
User::where('user_id', 1)->update( array('username'=>'admin', 'status'=>'active') );
Where "User" is the model name of "users" table.
其中“User”是“users”表的模型名称。
回答by Rahul
You are using raw query , it can be done as
您正在使用原始查询,可以这样做
update users set username = "admin", status = "active" where user_id = 1
更新用户设置 username = "admin", status = "active" where user_id = 1
DB::table('users')
->where('user_id',1)
->update(['username'=>'admin','status'=>'active']);
OR
或者
$username = "admin";
$status = "active";
DB::update(DB::RAW('update users set username = ' .$username. ',
status = '.$status.' where user_id = ?' ,['1']));
回答by Shakti Phartiyal
The better way to do that with laravel query builder is:
使用 laravel 查询构建器做到这一点的更好方法是:
DB::table('users')
->where('user_id', 1)
->update(['username' => $username, 'status' => $status]);
回答by Saad Bhutto
The correct ways to Call DB::updateyou need something like this
调用DB::update的正确方法你需要这样的东西
$username = "admin";
$status = "active";
DB::update('update users set username = ? , status = ? where user_id = ?', [$username , $status , 1]);
This should return number of rows affected
这应该返回受影响的行数

