drab
drab

Reputation: 484

Laravel Eloquent whereTime() shows null

I have this query to get the events after the the current time and it shows as null. I think the whereTime condition is not working or I have a typo.

$query = Model::where('id', $request->id)
                ->where('status_id', 1)
                ->whereTime('event_date', '>=', Carbon::now())
                ->first();

When I don't have the whereTime condition and check the results, it shows this result

$query = Model::where('id', $request->id)
                ->where('status_id', 1)
                // ->whereTime('event_date', '>=', Carbon::now())
                ->first();

dd(Carbon::now()->format('Y-m-d H:i:s'), $query->event_date);

Result

"2020-04-15 20:07:43"
"2020-05-23 18:00:00"

Upvotes: 4

Views: 8871

Answers (1)

Lokman Hosen
Lokman Hosen

Reputation: 482

If your date like this "2021-03-21 13:33:52" then separate date and time like below

$date = Carbon::parse($timestamp)->format('Y-m-d');
$time = Carbon::parse($timestamp)->toTimeString();

Now you got date and time separated and write query to desire model like below:

User::where('status', 1)->whereDate('created_at', '>=', $date)
      ->whereTime('created_at', '>', $time)->get();

Hope this will help you. This works for me

Upvotes: 9

Related Questions