sunel / eav

Entity–attribute–value model (EAV) for Laravel Artisan
https://sunel.github.io/eav/
143 stars 39 forks source link

Retrieve all attributes available for all products belonging to a category #27

Closed joaopmmartins closed 5 years ago

joaopmmartins commented 5 years ago

I am building a dynamic filter for products but need to create an array (to build the search form first) with all the (filterable) attributes from products belonging to a category. Can you point me in the right direction?

Product model:

public function attributes() {
        return $this->hasMany('Eav\Attribute', 'entity_id');
    }

public function categories() {
        return $this->belongsToMany('App\Category', 'category_product', 'product_id', 'category_id');
    }
sunel commented 5 years ago

use Eav\Model;

class Products extends Model
{
    const ENTITY  = 'product';
    public function categories()
    {
        return $this->belongsToMany(Category::class, 'product_category', 'product_id', 'category_id');
    }
}

$product = new Products();

# instance of Eav\Entity
$entity = $product->baseEntity();

$entity->load(['attributes'']);

# Collection of all Attributes to the enitity
$entity->attributes
joaopmmartins commented 5 years ago

thanks.