Reputation: 2554
I have these tables:
Category -> Line -> Product -> Filter
One product has many filters
One product has one category and one line
I need to get all the filters for one category. Today, through relationships between the tables, I can get all the products from on category and all the filters from one product:
// All the products
$category->products
// All the filters
$product->filters
//or
$category->products->first()->filters
Now, I need to get the filters by category, something like:
$category->filters
// or
$category->products->filters
A small brief:
How to get all the filters by Category?
Upvotes: 0
Views: 1435
Reputation: 5186
In your Category
model:
public function filters()
{
return Filter
::join('filter_product', 'filter.id', '=', 'filter_product.filter_id')
->join('products', 'filter_product.product_id', '=', 'products.id')
->join('categories', 'products.category_id', '=', 'categories.id')
->where('categories.id', $this->id);
}
public function getFiltersAttribute()
{
if (!$this->relationLoaded('products') ||
!$this->products->first()->relationLoaded('filters')) {
$this->load('products.filters');
}
return collect($this->products->lists('filters'))->collapse()->unique();
}
Then you will be able to use it like this:
$category->filters
To guarantee unique filter
results, you need to pass the unique fields, for ex. id
:
return collect($this->products->lists('filters'))->collapse()->unique('id');
Upvotes: 3
Reputation: 688
If I understood correctly, you need the with clause:
$category->with(products.filters)->get()
Upvotes: 0