Skip to content
Advertisement

Filtering Laravel datetime field for values after a certain time

I have an eloquent model that has a “start_time” field, which is of type dateTime, and I want users to be able to filter instances of the model based on time of day. So for example, the “start_time” field is in the format “2021-02-17 21:52:12” and I want the user to be able to send “08:00:00”, and then filter all instances of the model so that only instances where the time portion of start_time is beyond 08:00:00. So “2021-01-10 07:59:59” would not appear while “2020-12-12 08:01:00” would appear.

I’m trying to do something like this, but it should only consider the time element:

if ($request->has('start_time')) {
    $return_shifts->where('start_time', '>', $request->start_time);
}

How would I correctly apply this filter?

Advertisement

Answer

You can use whereTime method of laravel.

$return_shifts->whereTime('start_time', '>', $request->start_time);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement