cutups's avatar

Using eloquent ::whereDate

I'm looking to query my model and get objects that have a start_at time that matches a specific date regardless of the time.

It looks to me like this should work, but it returns nothing:

$events = Event::whereDate('start_at','=', '2016-03-11')
                    ->paginate();

However, if i do this:

$events = Event::where('start_at','=', '2016-03-11 22:00:00')
                    ->paginate();

I get an event with that exact start time. I know I can do this with a more complex query - but am I just mis-understanding how whereDate is supposed to work?

0 likes
2 replies
ChristopherSFSD's avatar

Are you doing something like foreach($events as $event) and checking? Or you can also do $events->count() to see the total there.

I played around with this in Tinker and it seemed to work well for me. The trick was that if ->paginate() vs ->get(), Tinker just shows a pagination object. You have to spin through that object to get the results.

morloderex's avatar

@cutups

My guess would be that you are accessing a magic method where the property hasnit been set.

Instead of:

$events = Event::whereDate('start_at','=', '2016-03-11')
                    ->paginate();

Try this:

$events = Event::whereStartAt( '2016-03-11')->paginate();

Please or to participate in this conversation.