Laravel eloquent search on fields of related model

后端 未结 2 1950
悲哀的现实
悲哀的现实 2020-12-13 01:10

I have an eloquent models as,

User : users(id, username, password, email, status)

Profile : profiles(id, user

2条回答
  •  隐瞒了意图╮
    2020-12-13 02:03

    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

提交回复
热议问题