繁体   English   中英

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

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

我想将此查询转换为雄辩的laravel,

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“ 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

任何想法?

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

要么

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

仅当您要查找单列在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应该使用这样->whereBetween('loc_lng', array(-0.24272918701172, -0.24272918701172))其第一个参数是列名,第二个是该区域。

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

您可以使用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]))与多个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);

我希望这对您有用:)

暂无
暂无

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

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