sunel / eav

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

Get value attributes by BelongsToMany #32

Closed livevasiliy closed 5 years ago

livevasiliy commented 5 years ago

Hi @sunel !

Can you help, I have two models. First AdminProduct - base model holds info about the product. The second model - It's Eav model AttributeProducts. Below my source code.

AdminProduct model

image

AttributeProducts model

image

And I have pivot table admin_product_attr image

Now I want to find the product by id with all attributes attached to a single product. I can get inverse by AttributeProducts all attributes & product.

sunel commented 5 years ago

@livevasiliy

Check this https://github.com/sunel/eav/blob/master/tests/Feature/Issue10Test.php

If you still have issue let me know i will give more info

livevasiliy commented 5 years ago

It's not solutions to my case. I need via relationship attrs get all attributes. I expect in results get the object to hold data about product and object attrs hold list attributes attached to the product by id. Your test-case the solution returned list attributes without data about model Designs.

If your use test-case I want to get list attributes model Designs use relationships cars return all value attributes attached to Design by id model Design

sunel commented 5 years ago

@livevasiliy

If i am correct you don't require two seprate models.

\Eav\Model is extended from \Illuminate\Database\Eloquent\Model so you can use the \Eav\Model as regular model.

Now to your example

class AdminProduct extends \Eav\Model
{
    const ENTITY  = 'admin_products';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = "admin_products";
}

in you db just update the entity_code field in entities table from attrs to admin_products;

Ref this https://sunel.github.io/eav/master/model.html#introduction

livevasiliy commented 5 years ago

@sunel

I wanna separate on two models because of I doing API for my Front-end and to be easy my Front-end developer to understand where attributes this product.

P.S: I make how you say me. I made extend from Eav\Model in my model AdminProduct.

livevasiliy commented 5 years ago

@sunel

I made a decision. I will make a collection in which I will store the values that I will receive separately and will give this collection to the frontend.

livevasiliy commented 5 years ago

@sunel

If I use model AdminProduct as Eav model. How I will handle creating new attributes because of my controller now creating products i.e I fill fields like name, barcode. Do I need me to create a new function in my controller via this function handle form for creating new attributes?

sunel commented 5 years ago

@livevasiliy

Yes