Q:

Laravel multiple Modles query

hey i have looked at quite a few articles now but i think my knowledge hinders me from adapting any example so here i am, hoping you can help:
What i want: Have an eloquent query which looks in 2 tables
What i have:
$myProfile = \App\User::with('friends')->where('friend_type','2')->get();

is my wished for result.
In my User Model i have the following:
    public function friends()
{
    return $this->hasMany(Friend::class);
}

and in my friends model:
    public function user()
{
    return $this->belongsTo(User::class);
}

also what i have tried, too:
$myProfile = \App\User::with(['friends'])->where('friend_type','2')->get();

or
$myProfile = \App\User::with('friends')->where('friends.friend_type',2)->get();

but the error i receive remains the same:
Column not found: 1054 Unknown column 'friends.friend_type' in 'where clause' (SQL: select * from `users` where `friends`.`friend_type` = 2 and `users`.`deleted_at` is null)

thanks very much in advance

A:

If you want to select all users whose friend_type is 2 then you should use whereHas. It would be like
\App\User::whereHas('friends', function($q){
  $q->where('friends.friend_type',2)
})->get();

