Laravel Eloquent日期范围 - 两个日期之间的查询

时间:2023-01-06 08:36:05

I'm creating a report page and what I wanted to do is to show reports from specific date to specific date. My current code is:

我正在创建一个报告页面,我想要做的是显示从特定日期到特定日期的报告。我目前的代码是:

$now = date('Y-m-d');
$reservations = Reservation::where('reservation_from', $now)->get();

What this does is to select * from table where reservation_from = $now. I have a query here but I don't know how to convert it to eloquent query. This is my code:

这样做是从表中选择*,其中reservation_from = $ now。我在这里有一个查询,但我不知道如何将其转换为雄辩的查询。这是我的代码:

SELECT * FROM table WHERE reservation_from BETWEEN '$from' AND '$to

How can I convert the code about to eloquent query? Thank you in advance.

如何将代码转换成雄辩的查询?先谢谢你。

4 个解决方案

#1


50  

The whereBetween method verifies that a column's value is between two values.

whereBetween方法验证列的值是否在两个值之间。

$from = date('2018-01-01');
$to = date('2018-05-02');

Reservation::whereBetween('reservation_from', [$from, $to])->get();

If you would like to add more condition then you can use orWhereBetween. If you would like to exclude a date interval then you can use whereNotBetween.

如果您想添加更多条件,则可以使用orWhereBetween。如果要排除日期间隔,则可以使用whereNotBetween。

Reservation::whereBetween('reservation_from', [$from1, $to1])
  ->orWhereBetween('reservation_to', [$from2, $to2])
  ->whereNotBetween('reservation_to', [$from3, $to3]);
  ->get();

Other useful where clauses: whereIn, whereNotIn, whereNull, whereNotNull, whereDate, whereMonth, whereDay, whereYear, whereTime, whereColumn, whereExists, whereRaw.

其他有用的where子句:whereIn,whereNotIn,whereNull,whereNotNull,whereDate,whereMonth,whereDay,whereYear,whereTime,whereColumn,whereExists,whereRaw。

Laravel docs about Where Clauses.

Laravel关于Where子句的文档。

#2


5  

Try this:

尝试这个:

Since you are fetching based on a single column value you can simplify your query likewise:

由于您基于单个列值进行提取,因此您可以同样简化查询:

$reservations = Reservation::whereBetween('reservation_from', array($from, $to))->get();

Retrieve based on condition: laravel docs

根据条件检索:laravel docs

Hope this helped.

希望这有帮助。

#3


4  

The following should work:

以下应该有效:

$now = date('Y-m-d');
$reservations = Reservation::where('reservation_from', '>=', $now)
                           ->where('reservation_from', '<=', $to)
                           ->get();

#4


4  

Another option if your field is datetime instead of date (although it works for both cases):

如果您的字段是日期时间而不是日期,则另一个选项(尽管它适用于两种情况):

$fromDate = "2016-10-01";
$toDate   = "2016-10-31";

$reservations = Reservation::whereRaw("reservation_from >= ? AND reservation_from <= ?", 
    array($fromDate." 00:00:00", $toDate." 23:59:59")
)->get();

#1


50  

The whereBetween method verifies that a column's value is between two values.

whereBetween方法验证列的值是否在两个值之间。

$from = date('2018-01-01');
$to = date('2018-05-02');

Reservation::whereBetween('reservation_from', [$from, $to])->get();

If you would like to add more condition then you can use orWhereBetween. If you would like to exclude a date interval then you can use whereNotBetween.

如果您想添加更多条件,则可以使用orWhereBetween。如果要排除日期间隔,则可以使用whereNotBetween。

Reservation::whereBetween('reservation_from', [$from1, $to1])
  ->orWhereBetween('reservation_to', [$from2, $to2])
  ->whereNotBetween('reservation_to', [$from3, $to3]);
  ->get();

Other useful where clauses: whereIn, whereNotIn, whereNull, whereNotNull, whereDate, whereMonth, whereDay, whereYear, whereTime, whereColumn, whereExists, whereRaw.

其他有用的where子句:whereIn,whereNotIn,whereNull,whereNotNull,whereDate,whereMonth,whereDay,whereYear,whereTime,whereColumn,whereExists,whereRaw。

Laravel docs about Where Clauses.

Laravel关于Where子句的文档。

#2


5  

Try this:

尝试这个:

Since you are fetching based on a single column value you can simplify your query likewise:

由于您基于单个列值进行提取,因此您可以同样简化查询:

$reservations = Reservation::whereBetween('reservation_from', array($from, $to))->get();

Retrieve based on condition: laravel docs

根据条件检索:laravel docs

Hope this helped.

希望这有帮助。

#3


4  

The following should work:

以下应该有效:

$now = date('Y-m-d');
$reservations = Reservation::where('reservation_from', '>=', $now)
                           ->where('reservation_from', '<=', $to)
                           ->get();

#4


4  

Another option if your field is datetime instead of date (although it works for both cases):

如果您的字段是日期时间而不是日期,则另一个选项(尽管它适用于两种情况):

$fromDate = "2016-10-01";
$toDate   = "2016-10-31";

$reservations = Reservation::whereRaw("reservation_from >= ? AND reservation_from <= ?", 
    array($fromDate." 00:00:00", $toDate." 23:59:59")
)->get();