Laravel: How To Display Has Many Through Many To Many Based On The Number Of Nested Relationships
I am facing a small difficulty in finding the best way to get nested relationships through two many-to-many many relationships.
I have 3 models: Article, Keyword and Subject with the following relationships:
Article <-> Keyword (many to many);
Article <-> Subject (many to many).
What I am trying to do is getting one subject's keywords based on all the subject's articles so I can display them as a bubble with scalable size.
The code so far:
$subject = Subject::with('articles.keywords')->find($id);
$keywordsArray = $subject->articles->pluck('keywords');
$keywords = (new Collection($keywordsArray))->collapse()->groupBy('id');
return $keywords; // returns the collection with all the duplicate keywords
return $keywords->unique('id') // returns unique keywords.
I know to return the count mapping from the collection like this:
$keywordsCount = (new Collection($keywordsArray))->collapse()->groupBy('id')->map(function($item, $key){
return $item->count();
});
return $keywordsCount; // returns ex: {"1":2,"2":2,"3":2,"6":1}
Everything works, the only question that remains is: how do I merge the unique keywords with $keywordsCount, or, what is the best way to utilize these 2 so I can resize the keyword's bubble based on that count.
Many thanks in advance and godspeed.
Answer
It depends on how often you'll need to retrieve that merged information, but for that collection you've retrieved now you can always simply assign the count as a temporary attribute to every current keyword
instance in the collection.
Alternatively, you can also just pack both up in an array, for which matter I'd simply do something like using the zip()
collection method:
return $keywords->unique('id')->zip($keywordsCount);
which will create a collection of coupled arrays like [['keyword1', 1], ['keyword2', 3], ...]
thus merging the information as you wished.
Related Questions
- → "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