FewFlyBy FewFlyBy - 1 year ago 173
PHP Question

Laravel Eloquent Date Range

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:

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

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

Answer Source

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


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

If you would like to add more condition, you can use orWhereBetween.

$reservations = Reservation::whereBetween('reservation_from', [$from_from, $to_from])
                ->orWhereBetween('reservation_to', [$from_to, $to_to])

You should know about: whereNotBetween, whereIn, whereNotIn, whereNull, whereNotNull

Laravel docs about where.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download