我一直在尝试使用update
子句编写大量where
查询,并且我发现Laravel 5.3 Eloquent Query Builder会出现语法错误。
查询如下:
$query = $this->model
->where('state', "pending")
->whereRaw('created_at <= NOW() - INTERVAL 12 HOUR')
->update(['state' => "timeout"]);
将其翻译为:
update `orders`
set `state` = timeout, `updated_at` = 2016-09-21 21:47:39
where `state` = pending and created_at <= NOW() - INTERVAL 12 HOUR
此查询会一直失败,因为列state
和updated_at
的值不是用单引号编写的。
如何强制Laravel编写正确的单引号?
答案 0 :(得分:1)
也许试试where('created_at', DB::raw('NOW() - ..'));
你为什么要用单引号加注? Laravel会自动转义值。
答案 1 :(得分:0)
使用类似
的内容$results = MyModel::where('user_id', Auth::user()->id)
->where('created_at','>=',\DB::raw('"'.$from.'"'))
->where('created_at','<=',\DB::raw('"'.$to.'"'))
->orderby('id','desc')
->paginate(15);
希望这有帮助。