rinvex / laravel-attributes

⚠️ [ABANDONED] Rinvex Attributable is a robust, intelligent, and integrated Entity-Attribute-Value model (EAV) implementation for Laravel Eloquent, with powerful underlying for managing entity attributes implicitly as relations with ease. It utilizes the power of Laravel Eloquent, with smooth and seamless integration.
MIT License
434 stars 104 forks source link

Gathering Attributes for Specified Entity #78

Closed dhildreth closed 5 years ago

dhildreth commented 5 years ago

Is there a way to filter out the attributes that are tied to a specified entity?

To get a list of all attributes, we have:

$attributes = Attribute::all()

But, what if we just want attributes that belong to an entity, like this:

$partAttributes = Attribute::entities('part')->all();
$productAttributes = Attribute::entities('product')->all();

Similar to $product->getEntityAttributes(), but without needing to fetch an entity first. Any hints?

dhildreth commented 5 years ago

In my App\Attribute class which extends Rinvex\Attributes\Models\Attribute, I just added a scope to deal with it.

    public function scopeInEntities($query, $entity_type)
    {
        return $query->whereHas('entities', function($query) use ($entity_type) {
            $query->whereIn('entity_type', (array)$entity_type);
        });
    }