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

How to work with Collections ? #156

Closed dani7115 closed 3 years ago

dani7115 commented 3 years ago

Hi.

I am trying this package. But I found out it isn't properly explained in the documentation that how to work with collectionValues. I have already tried to extend / include this class to my model. But still I don't get anything.

Firstly I was facing issue while trying this $product = \App\Models\Product::find(1); $product->size_3->add('xs');

It was giving me error Error Call to a member function add() on null

Then later I discovered by doing some search. That the attributes should have is_collection = 1 while you create them. Where this thing isn't mentioned in the documentation. After doing this, The error was gone. But I didn't see any changes in DB. Where and how its added ?

So I moved onto further things in the doc.

Here below ( screen shot ) I have problem in performing this as I've explained in the first line of this post.

Screen Shot 2021-05-18 at 2 17 13 PM

is there any proper good example available for doing it ?

Please let me know..

Thanks

paligiannis commented 3 years ago

Yes, please carefully read the documentation. Please check some code below

create attribute first

Create attribute app('rinvex.attributes.attribute')->create([ 'slug' => 'needs_batteries', 'type' => 'boolean', 'name' => 'item_needs_batteries', 'entities' => ['App\Product'], ]);

get the item you need

$p = \App\Product::find(1);

then you can just simply call item item_needs_batteries $p->item_needs_batteries = true;

then save it like a boss $p->save();

this below part is really usefull also

$attributes = ['needs_batteries', 'test_null'];
      foreach ($attributes as $attribute) {
         dump($p->$attribute);
      }
dani7115 commented 3 years ago

Hi.

Thanks for the reply. I've already tried that. And it works well. It does not matter that the item_needs_batteries is belongs to the main product table, or it's an EAV attribute. It works always.

But basically I need to understand this line.

**_$entity->cities->add('Alexandria');_**

Can u let me know, How can I achieve this ? Because I could not make it work

Omranic commented 3 years ago

If cities is_collection, then it automatically inherits from Laravel's Collection class, and thus it has the method add. So when you execute that line above $entity->cities->add('Alexandria'); it will add a new value to cities collection, and save it as one of its values. I hope that helps.

Although, I did a complete rewrite for the whole package and changed how collections work. In the new refactor, it actually uses normal relationships, and should be easy and straightforward like default Laravel relationships. Although, that refactor is incomplete.

I'm still not happy with the overall performance (I believe we can reduce number of executed queries), if you want to check it out, see https://github.com/rinvex/laravel-attributes/tree/refactor-to-native-laravel-relationships

Currently no plans to merge that rewrite, but hopefully sometime I can get it to a stable state, improve performance and release it. Any help with that branch would be much appreciated! 🙂

stpmom commented 3 years ago

Hi I have issue with saving model when it has linked ValueCollection

for example $product = Product::create(); $attribute_material = app('rinvex.attributes.attribute')->whereSlug('material')->first(); $product->composition = (new \Rinvex\Attributes\Support\ValueCollection())->link($product, $attribute_material) ->add(['Cotton 80%', 'Polyester 20%']);

So I can read values with $product->composition->pluck('content')->toArray() but when I tring to save model with $product->save() I get "SQLSTATE[HY000]: General error: 1 no such column"

Omranic commented 2 years ago

More info on how to use value collections here https://github.com/rinvex/laravel-attributes#rinvexattributessupportvaluecollection