我需要sql查询,但我需要使用Eloquent:
"SELECT * FROM Pass WHERE serial_number=$registered_serial_numbers AND (updated_at IS NULL OR updated_at >= $passesUpdatedSince)"
我尝试但有错误:
$registered_passes = Pass::where('serial_number', $registered_serial_numbers)->where("updated_at IS NULL OR updated_at >= $passesUpdatedSince")->get();
我应该如何在Eloquent中做到这一点?
答案 0 :(得分:1)
两种方式:
1)使用whereRaw()
$registered_passes = Pass::whereIn('serial_number', $registered_serial_numbers)->whereRaw('updated_at IS NULL OR updated_at >= ?', $passesUpdatedSince)->get();
2)使用闭包来做AND(OR)
$registered_passes = Pass::whereIn('serial_number', $registered_serial_numbers)->where(function($query){
$query->whereNull('updated_at')->orWhere('updated_at', '>=', $passesUpdatedSince);})->get();
https://laravel.com/docs/5.3/queries#where-clauses
查看“参数分组”部分
答案 1 :(得分:0)
查询应该是这样的:
$registered_passes = Pass::where("updated_at", $passesUpdatedSince)->whereIn('serial_number',$registered_serial_numbers)->get();
您对 whereIn
子句使用where in
方法,并确保$registered_serial_numbers
是一个值数组。
我的坏,误读了。如果您对序列号没问题,只需执行whereRaw
,如下所示:
$registered_passes = Pass::where('serial_number',$registered_serial_numbers)->whereRaw('updated_at IS NULL OR updated_at >= ?', $passesUpdatedSince)->get();