我想将此查询转换为laravel eloquent,
select * from schedule where (now() between start_date and end_date);
我尝试使用whereBetween,但是我遇到了一些错误。
$schedule = Schedule::whereBetween(Carbon::now(), ['start_date', 'end_date'])->get();
错误看起来像这样
Connection.php第647行中的QueryException: SQLSTATE [42S22]:未找到列:1054未知列' 2017-06-01 06:17:30'在' where子句' (SQL:select * from
schedule
其中2017-06-01 06:17:30
在start_date和end_date之间)
任何想法?
答案 0 :(得分:2)
$schedule = Schedule::where('start_date', '<=', Carbon::now())
->where('end_date', '>=', Carbon::now())
->get();
或者
$schedule = Schedule::whereRaw('(now() between start_date and end_date)')->get();
答案 1 :(得分:1)
whereBetween应使用like this,->whereBetween('loc_lng', array(-0.24272918701172, -0.24272918701172))
,其第一个参数是列名,第二个参数是区域。
在您的情况下,您可以使用->where('start_date', '<' Carbon::now())->where('end_date', '>' Carbon::now());
答案 2 :(得分:1)
仅当您要查找单个列在2个值之间的行时才使用whereBetween,您要执行的操作是:
[
{
"drdsc":"DSCDS0101101",
"bkpsets":[
{
"backpset":"Backup1",
"srvrolenotes":"",
"setsize1notes":"",
"setsize2notes":""
},
{
"backpset":"Backup2",
"srvrolenotes":"",
"setsize1notes":"",
"setsize2notes":""
},
{
"backpset":"Backup3",
"srvrolenotes":"",
"setsize1notes":"",
"setsize2notes":""
}
]
},
{
"drdsc":"DSCDS0202202",
"bkpsets":[
{
"backpset":"Backup4",
"srvrolenotes":"",
"setsize1notes":"",
"setsize2notes":""
}
]
}
]
答案 3 :(得分:1)
$from = $request->from;
$to = $request->to;
$title="Sales From: ".$from." To: ".$to;
$sales = Sale::whereBetween('created_at', [$from.' 00:00:00',$to.' 23:59:59'])->get();
答案 4 :(得分:0)
您可以使用whereBetween('date_field',[$from_date, $to_date])
来获取两个日期之间的数据,但是它应该是数组格式。
$products = Sale::with('products')
->whereBetween('date',[$from_date, $to_date])
->get();
return response()->json($products);
您还可以将whereBetween(whereBetween('date_field',[$from_date, $to_date]))
用于多个条件,例如:
$products = Sale::with('products')
->whereBetween('date',[$from_date, $to_date])
->where(function ($query) use ($search) {
$query->where('employee_id', 'like', '%'.$search.'%')
->orWhere('employee_name', 'like', '%'.$search.'%');
})
->get();
return response()->json($products);
我希望这对您有用:)