Eloquent conditional where filter
Since Laravel 5.2.27, you can use conditional clauses
For example:
->when($gender, function($query) use ($gender) {
return $query->where('gender', '=', $gender);
})
->get();
You could use a dynamic scope
class User extends Eloquent {
public function scopeGender($query, $gender)
{
if ($gender) {
return $query->whereGender($gender);
}
return $query;
}
}
Then throughout your application
...->gender($gender)->get();