Laravel - 使用 eloquent 在开始日期和结束日期之间选择行

Ahm*_*yid 4 php laravel eloquent

我想将此查询转换为 Laravel eloquent,

select * from schedule where (now() between start_date and end_date);
Run Code Online (Sandbox Code Playgroud)

我尝试使用 whereBetween,但出现了一些错误。

$schedule = Schedule::whereBetween(Carbon::now(), ['start_date', 'end_date'])->get();
Run Code Online (Sandbox Code Playgroud)

错误看起来像这样

Connection.php 第 647 行中的 QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column '2017-06-01 06:17:30' in 'where clause' (SQL: select * from schedulewhere 2017-06-01 06:17:30between start_date and end_date)

任何的想法?

小智 14

$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();
Run Code Online (Sandbox Code Playgroud)


San*_*esh 5

$schedule = Schedule::where('start_date', '<=', Carbon::now())
    ->where('end_date', '>=', Carbon::now())
    ->get();
Run Code Online (Sandbox Code Playgroud)

或者

$schedule = Schedule::whereRaw('(now() between start_date and end_date)')->get();
Run Code Online (Sandbox Code Playgroud)


Mat*_*rre 5

whereBetween 仅在您想要查找单列介于 2 个值之间的行时使用,您想要做的是:

$now = Carbon::now();
$schedule = Schedule::where('start_date', '<=', $now)
    ->where('end_date', '>=', $now)
    ->get();
Run Code Online (Sandbox Code Playgroud)