Can not access the 3rd table in laravel 5 in 3 tables have relationships

59 views Asked by At

I have these table

- users table

  id  name  password

- keywords table

  id   name  user_id

- groups table

  id  name  keyword_id

I want to retrieve logged in user groups

$groups = Group::with(['keyword' => function($query){
              $query->where('user_id' , Auth::user()->id)
          }]);

but all groups return NOT that groups for that user.

1

There are 1 answers

0
chanafdo On BEST ANSWER

You should use whereHas

$groups = Group::whereHas('keyword', function($query)
{
    $query->where('user_id' => Auth::user()->id);
})->get();