wrong and right way to write eloquent
What if you have and-or mix in your SQL query, like this:
... WHERE (gender = 'Male' and age >= 18) or (gender = 'Female' and age >= 65)
How to translate it into Eloquent? This is the wrong way:
$q->where('gender', 'Male');
$q->orWhere('age', '>=', 18);
$q->where('gender', 'Female');
$q->orWhere('age', '>=', 65);
The order will be incorrect. The right way is a little more complicated, using closure functions as sub-queries:
$q->where(function ($query) {
$query->where('gender', 'Male')
->where('age', '>=', 18);
})->orWhere(function($query) {
$query->where('gender', 'Female')
->where('age', '>=', 65);
})