Search code examples
phplaravelmodeleloquentrelationships

Select all Filters from Products which belongs to one Category in Laravel


I have these tables:

  • Category
  • Line
  • Product
  • Filter
  • Product_filter

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:

  • A category has products who have filters

How to get all the filters by Category?


Solution

  • 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');