WHERE method not working with valid query Laravel

Advertisements

I know that similar questions have been asked, but none of them have solutions that work for me.

I have a table called events with a column titled "event_finish_time," a Timestamp. I need to get all events that end after a certain timestamp (usually now()->format(...)). I have the following code written as a test:

$timestamp = "2020-11-22 13:00:00";
$events = Events::where('event_finish_time', '>=', '"'.$timestamp.'"');
dd($events->get("*")->toArray());

This code returns [].
Using toSql() and getBindings(), I get:
select * from `events` where `event_finish_time` >= ? and "2020-11-22 13:00:00"
The combination (select * from `events` where `event_finish_time` >= "2020-11-22 13:00:00") runs perfectly fine from phpMyAdmin, and returns the correct result.
Setting the binding to 'TIMESTAMP("' . now() . '")' doesn’t help with the php query and does not affect the direct SQL one.
What could be the cause of this issue? I have checked the spelling of all the tables and columns.

>Solution :

The correct syntax for comparing timestamps in Laravel is simpler than what you have. You don’t need to wrap the timestamp in double-quotes.

$timestamp = "2020-11-22 13:00:00";
$events = Events::where('event_finish_time', '>=', $timestamp)->get();
dd($events->toArray());

If this doesn’t solve the issue, you might also want to check if there are any date format discrepancies between the timestamp in your database and the one you’re comparing it to.

Additionally, ensure that the event_finish_time column in your events table is of the TIMESTAMP type or a compatible date/time type.

Leave a ReplyCancel reply