0

I have a simple many-to-many polymorphic relationship (if such thing is possible):

Authors: On Blog we have Posts => Authorable On Magazine, Articles => Authorable

Both relationships work as expected/documented. What I need is to fetch All authors for a specific Post category

All I have is: Post::category('blue') Collection (category being a scope). Based on that, what is the best way to get Authors that wrote "Blue Posts" ?

Fernando Barrocal
  • 12,584
  • 9
  • 44
  • 51

1 Answers1

2

You'll want to look into using the has() and whereHas() query methods within your scope. See Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean? for a detailed explanation on using them.

Basically would look something like this:

$authorsOfBluePosts = Author::whereHas('posts', function ($postsQuery) {
    $postsQuery->whereHas('category', function ($categoryQuery) {
        $categoryQuery->where('name', 'blue');
    });
})->get();
Community
  • 1
  • 1
LukeTowers
  • 1,262
  • 7
  • 14