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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44300126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:04:14  来源:igfitidea点击:

Laravel - select row between start date and end date using eloquent

phplaraveleloquent

提问by Ahmad Jamil Al Rasyid

I want to convert this query into laravel eloquent,

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

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 schedulewhere 2017-06-01 06:17:30between start_date and end_date)

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)

any idea?

任何的想法?

回答by THEMBO CHARLES LWANGA

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

回答by Sandeesh

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

回答by Mathieu Ferre

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 :

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

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

回答by Kris Roofe

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

回答by Zia Khan

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 :)

我希望这对你有用:)