jeudi 19 janvier 2017

Laravel 5.3 get custom Array (Object) from Eloquent relationships

I have Products, Attributes and Categories. I want to create custom attributes array without using foreach. I know I can get that done with foreach but I want to do it with Eloquent query.

This is my table diagram: Attributes tables diagram

I want to create array (Eloquent returned Object) like this:

$attributes = [
        'attribute1' => [
            'attribute1_property1' => 'attribute1_property1_count_in_all_products',
            'attribute1_property2' => 'attribute1_property2_count_in_all_products',
            'attribute1_property3' => 'attribute1_property3_count_in_all_products',
        ],
        'attribute2' => [
            'attribute2_property1' => 'attribute1_property1_count_in_all_products',
            'attribute2_property2' => 'attribute1_property2_count_in_all_products',
            'attribute2_property3' => 'attribute1_property3_count_in_all_products',
        ],
        ...
    ]

I have models Product, Attribute, AttributeOption.

Product::whereHas('category', $category)->with('attributeOtions.attributes')->get();

With above code I get different collection and what can I do to get that array? Thank you in advance.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2iXIrBm
via IFTTT

Aucun commentaire:

Enregistrer un commentaire