简体   繁体   English

Laravel-使用雄辩的语言在开始日期和结束日期之间选择行

[英]Laravel - select row between start date and end date using eloquent

I want to convert this query into laravel eloquent, 我想将此查询转换为雄辩的laravel,

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

I tried using whereBetween, but I got some error. 我尝试使用whereBetween,但是出现了一些错误。

$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) Connection.php第647行中的QueryException:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ 2017-06-01 06:17:30”(SQL:从schedule中选择* 2017-06-01 06:17:30在开始日期和结束2017-06-01 06:17:30之间2017-06-01 06:17:30

any idea? 任何想法?

$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 : 仅当您要查找单列在2个值之间的行时,才使用whereBetween:

$now = Carbon::now();
$schedule = Schedule::where('start_date', '<=', $now)
    ->where('end_date', '>=', $now)
    ->get();
$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. whereBetween应该使用这样->whereBetween('loc_lng', array(-0.24272918701172, -0.24272918701172))其第一个参数是列名,第二个是该区域。

In you situation you can use ->where('start_date', '<' Carbon::now())->where('end_date', '>' Carbon::now()); 在您的情况下,可以使用->where('start_date', '<' Carbon::now())->where('end_date', '>' Carbon::now());

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. 您可以使用whereBetween('date_field',[$from_date, $to_date])来获取两个日期之间的数据,但该数据应采用数组格式。

$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: 您还可以将whereBetween(whereBetween('date_field',[$from_date, $to_date]))与多个where条件一起使用,例如:

$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 :) 我希望这对您有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM