Level 10
Oct 30, 2017
3
Level 26
Query by date range in db date range fields
In my application I can add holidays for employers, setting date range (start_date, end_date).
In the frontend user can choose two dates- "date_from" and "date_to" and I should perform query which should return holidays for which at least one day of holiday is between that given dates (so return not only holidays that fits enitirely in that date- which I know how to do).
Unfortunately I dont have any idea how to construct query like that.
Level 26
Ok, I figured it out by myself. Here is the solution:
Holiday::where([
['start_date', '>=', $date_from->format('Y-m-d')],
['end_date', '<=', $date_to->format('Y-m-d')],
])
->orWhere([
['start_date', '>=', $date_from->format('Y-m-d')],
['end_date', '<=', $date_to->format('Y-m-d')],
])
->orWhere([
['start_date', '<=', $date_from->format('Y-m-d')],
['end_date', '>=', $date_to->format('Y-m-d')],
])
->get();
Please or to participate in this conversation.