Laravel Polymorphic Many To Many Fetch By Specific Association
Ad
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" ?
Ad
Answer
Ad
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();
Ad
source: stackoverflow.com
Related Questions
Ad
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms
Ad