Tutorial last revisioned on August 18, 2022 with Laravel 9
Eloquent is a great thing - you can build your query step-by-step and then call get() method. But sometimes it gets a little tricky for more complicated queries - for example, if you have multiple AND-OR conditions and you want to put brackets, how to do it properly?
Wrong way - easy to make mistake
Let's say we need to filter male customers aged 18+ or female customers aged 65+ (whatever the reason is, it's just a hypothetical example). Simple MySQL query would look something like this:
... WHERE (gender = 'Male' and age >= 18) or (gender = 'Female' and age >= 65)