Italo Borges Italo Borges - 1 year ago 172
PHP Question

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

// All the filters


Now, I need to get the filters by category, something like:

// or

A small brief:

  • A category has products who have filters

How to get all the filters by Category?

Answer Source

In your Category model:

public function filters()
    return Filter

        ::join('filter_product', '', '=', 'filter_product.filter_id')

        ->join('products', 'filter_product.product_id', '=', '')

        ->join('categories', 'products.category_id', '=', '')

        ->where('', $this->id);

public function getFiltersAttribute()
    if (!$this->relationLoaded('products') || 
        !$this->products->first()->relationLoaded('filters')) {

    return collect($this->products->lists('filters'))->collapse()->unique();

Then you will be able to use it like this:


To guarantee unique filter results, you need to pass the unique fields, for ex. id:

return collect($this->products->lists('filters'))->collapse()->unique('id');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download