[SOLVED] Get all available attributes with values for special collection on EAV

Issue

I’m using this package for deploying EAV model.

As any EAV system I have different table for storing varchar, boolean, text and integer value in my database, and attributes table for storing attribute.
I need to get all available attribute based on my posts list (collection).

For example a special list of posts

$posts = Post::where('category_id',2)->where('tag_id',4)->get();

On each post I have difference attribute with difference value, I need a way to get all attribute on this list (not all attribute on system) AND available value based on this collection.

I’m developing a filtering page for my posts and I need to show available attribute on sidebar with their available value to change.

Solution

UPDATE – Solution

Finlay I found a good solution for getting all attributes based on my query.

   $attributes = $posts
        ->leftJoin('attribute_boolean_values', 'attribute_boolean_values.entity_id', 'posts.id')
        ->leftJoin('attribute_varchar_values', 'attribute_varchar_values.entity_id', 'posts.id')
        ->join('attributes', function ($join) {
            $join->oron('attribute_boolean_values.attribute_id', 'attributes.id');
            $join->oron('attribute_varchar_values.attribute_id', 'attributes.id');
        })
        ->groupBy('attributes.id')
        ->select([
            'attributes.slug as slug',
            'attributes.type as type',
            'attributes.id as id',
        ])
        ->get();

Answered By – M Mehdizadeh

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *