Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Brackets Between "and" / "or" Conditions

Premium
3:04

In Eloquent, there's a pretty dangerous thing that you may encounter if you try to build a more complex query with and and or clauses.


Imagine the scenario where you want to filter the users with email_verified_at and some other condition with or. For example, we're filtering users with email_verified_at not null and where the day of created_at is equal to 4 or 5.

$users = User::whereNotNull('email_verified_at')
->whereDay('created_at', 4)
->orWhereDay('created_at', 5)
->get();
 
foreach ($users as $user) {
dump($user->id . ': ' . $user->name);
}

In the database, I have three users.

Two of them are with verified email, and all are created on the fourth or fifth day. What should this query return?

Probably two users, because we're querying the email_verified_at, which should be true for two out of three records. But the result is all three records:

Let's check the SQL query.

select * from "users"
where "email_verified_at" is not null
and strftime('%d', "created_at") = cast('04' as text)
or strftime('%d', "created_at") = cast('05' as text)

NOTE: The SQL query syntax is for SQLite.

If you know the theory of SQL, then the order of that sequence would be exactly this: email, and day, and then or day.

Which means...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

M
MicroDev ✓ Link copied!

Hi, I noticed a small issue in the example code:

$users = User::whereNotNull('email_verified_at')

->whereDay('created_at', 4)
	
->orWhere('created_at', 5) // **This line should use orWhereDay**
	
->get();
	

It looks like the orWhere method should be orWhereDay to correctly filter by day.

Thanks for sharing such great content!

M
Modestas ✓ Link copied!

Thank you for noticing this! Updated :)