I am working with the fullcalendar plugin and I am trying to fetch all the events in my database after and before a certain date. Full calendar paginates the events by adding paramaters called 'start' and 'end'. I am able to parse these in to Carbon instances like so:
Carbon::parse($data['start']);
Carbon::parse($data['end']);
Now this is working fine:
$events = $this->event->where('user_id', auth()->user()->id)
->get();
But this is not:
$events = $this->event->where('user_id', auth()->user()->id)
->where('start', '>=', Carbon::parse($data['start']))
->where('end', '<=', Carbon::parse($data['end']))
->get();
Why am I not getting events published between these two dates with the second approach. When I run the first approach, it works fine and fetches all the events and publishes them on the current page but unfortunately it is also fetching a whole lot of events that don't need fetching.
After listening to the DB queries, the event query was like:
select * from
events
whereuser_id
= ? andstart
>= ? andend
<= ?
This was a small mistake on my end where I was not setting the end date for the event in my DB and hence the query where all the events for the authenticated users was working correctly.
In fact the query for >= start
would also have worked alone but the the <= end
was creating as issue since end was null and hence Eloquent was not returning any models.