The 100 method verifies that a column's value is between
two values.
$from = date('2018-01-01');
$to = date('2018-05-02');
Reservation::whereBetween('reservation_from', [$from, $to])->get();
在某些情况下,您需要动态添加日期范围.根据@Anovative的 comments ,你可以这样做:
Reservation::all()->filter(function($item) {
if (Carbon::now()->between($item->from, $item->to)) {
return $item;
}
});
如果您想添加更多条件,则可以使用orWhereBetween
.如果要排除日期间隔,则可以使用whereNotBetween
.
Reservation::whereBetween('reservation_from', [$from1, $to1])
->orWhereBetween('reservation_to', [$from2, $to2])
->whereNotBetween('reservation_to', [$from3, $to3])
->get();
其他有用的where子句:whereIn
、whereNotIn
、whereNull
、whereNotNull
、whereDate
、whereMonth
、whereDay
、whereYear
、whereTime
、whereColumn
、whereExists
、whereRaw
.
Laravel docs about Where Clauses.