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

I want to convert this query into laravel eloquent,

select * from schedule where (now() between start_date and end_date);

I tried using whereBetween, but I got some error.

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

the error looks like this

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

any idea?

$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();

whereBetween should used like this, ->whereBetween('loc_lng', array(-0.24272918701172, -0.24272918701172)) whose first parameter is the column name, and the second is the region.

In you situation you can use ->where('start_date', '<' Carbon::now())->where('end_date', '>' Carbon::now());

$schedule = Schedule::where('start_date', '<=', Carbon::now())
    ->where('end_date', '>=', Carbon::now())
    ->get();

Or

$schedule = Schedule::whereRaw('(now() between start_date and end_date)')->get();

The whereBetween is used only when you want to find a row where a single column is between 2 values, what you want to do is :

$now = Carbon::now();
$schedule = Schedule::where('start_date', '<=', $now)
    ->where('end_date', '>=', $now)
    ->get();

You can use whereBetween('date_field',[$from_date, $to_date]) to fetch the data between two dates but it should be in the array format.

$products = Sale::with('products')
    ->whereBetween('date',[$from_date, $to_date])
    ->get();
return response()->json($products);

You can also use whereBetween(whereBetween('date_field',[$from_date, $to_date])) with multiple where conditions like:

$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);

I hope this will work for you :)