Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

WHERE method not working with valid query Laravel

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading