Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tankerkiller125's avatar

Get all Alerts where the expired date is null or above the current date

I've spent a good 2 or so hours debugging this already and I've attempted writing an actual MySQL query for this with no luck. What I have is a database table with alerts that have titles, descriptions, a type and an expiration date.

What I'm trying to do is get all of the alerts that either have no expiration date (they are non-expiring alerts) and all the alerts with expiration dates in the future. My current problem is that I can get alerts that have no expiration date but I either get no alerts with expiration in the future, or I get all the alerts that have expiration dates with the same year, even if the date is in the past.

The MySQL query that kind of works but really doesn't:

SELECT * FROM alerts WHERE (expires_at >= CURRENT_TIMESTAMP OR alerts.expires_at IS NULL)

Any help with this would be greatly appreciated!

0 likes
5 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Is expires_at a datetime field?

You need to use where(condition1)->orWhere(condition2)

$alerts = Alert::whereNull('expires_at')
                             -orWhere('expires_at' > Carbon\Carbon::now())
                             ->get();
bobbybouwmann's avatar

If general Mysql you can also do something like this

SELECT * FROM alerts WHERE expires_at >= NOW() OR expires_at IS NULL
tankerkiller125's avatar

@Snapey It is a timestamp field, however your query doesn't work saying: SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select * from alerts where expires_at is null or 1 is null)

tankerkiller125's avatar

@Snapey @bobbybouwmann Thanks guys for all the help, I found the issue, it seems that homestead and my machine are using different timezones and so they are different by several hours, so when I used PHPStorms editor it set the timestamp to my laptops time which is about 15 hours ahead of homestead.

Please or to participate in this conversation.