So ... I got those two eloquent models and a pivot table:
## Categories ##
- ID
- title
belongsToMany(Items)
## Items ##
- ID
- title
belongsToMany(Categories)
## Items_Categories ##
- items ID
- categories ID
I need a list of ALL items and highlight those which are in a given category.
# Example #
## Categorie #1 ##
- Item #1 (is in category)
- Item #2
- Item #3 (is in category)
- Item #4 (is in category)
- Item #5
## Category #2 ##
- Item #1 (is in category)
- Item #2
- Item #3
- Item #4 (is in category)
- Item #5
I feeld like I did this a hundred times in the past, but I can't figure out how to set this up. :-(
If there's a better solution on setting up those models / relationships I'm game.
// Get a single category with containing items
$category = Catgegory::whereNull('is_deleted')
->where('id', 1)
->with('items')
->first();
// Get all items
$allItems = Item::whereNull('is_deleted')
->get();
// Now what?
foreach ($allItems as $item) {
// Compare with category items?!
}