Laravel eloquent search on fields of related model

That’s where whereHas comes in handy:

$user = User::with('Profile')->where('status', 1)->whereHas('Profile', function($q){
    $q->where('gender', 'Male');
})->get();

Basically it adds the condition that the user needs to have a profile with gender = Male

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)