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

[Value Collection] Method Illuminate\Support\Collection::add does not exist. #70

Closed batFormat closed 3 years ago

batFormat commented 6 years ago

Method Illuminate\Support\Collection::add does not exist. Sample code,

// Entity Advert with load `eav`.
// protected $with = ['eav'];

$advert = App\Advert::find(1); 
// conditions my attribute, with is_collection (1)
$advert->conditions->add(['Alexandria', 'Cairo']);
$advert->save();

2018-09-22 18 50 36 2018-09-22 18 58 42

lxin87 commented 6 years ago

i got same issue!

app('rinvex.attributes.attribute')->create([
        'slug' => 'color',
        'type' => 'varchar',
        'name' => 'Product Size',
        'is_collection'=>1,
        'entities' => ['App\Product'],
]);

//Product model

public function newCollection(array $models = [])
{
    return new ValueCollection($models);
}

$product = Product::find(3);
$value = new Varchar(['content' => 'test two attribute', 'attribute_id' => 2, 'entity_type' => 'App\Product', 'entity_id' => 3]);
$article->color->push($value);
//$article->color->add($value); # Method Illuminate\Support\Collection::add does not exist. 
$article->save(); // it's not work

who can help me ?

lxin87 commented 6 years ago

i got same issue!

app('rinvex.attributes.attribute')->create([
        'slug' => 'color',
        'type' => 'varchar',
        'name' => 'Product Size',
        'is_collection'=>1,
        'entities' => ['App\Product'],
]);

//Product model

public function newCollection(array $models = [])
{
    return new ValueCollection($models);
}

$product = Product::find(3);
$value = new Varchar(['content' => 'test two attribute', 'attribute_id' => 2, 'entity_type' => 'App\Product', 'entity_id' => 3]);
$article->color->push($value);
//$article->color->add($value); # Method Illuminate\Support\Collection::add does not exist. 
$article->save(); // it's not work

who can help me ?

enmmmmm...

I found a way, which is not the same as described in the documentation, but solved my problem.

// Product model
// Creating an attribute is equivalent to creating a relationships, color is my created an attribute .

protected $with = ['eav','color']; 

//routes/web.php

$product = Product::find(3);
$product->getRelation('color')->add('red')
$product->save();
dd($product->toArray())

image

uphlewis commented 5 years ago

Like @lxin87 mentioned, you need to use $model->getRelation($attribute_slug) because rather than assigning a value to the dynamic property, you're accessing it in order to call a method, so the model is calling getAttribute() instead of setAttribute().

The returned type is Illuminate\Support\Collection rather than the expected Rinvex\Attributes\Support\ValueCollection, hence why ->add() does not exist.

IsraelOrtuno commented 5 years ago

Could you please update the issue and specify your Laravel version?

batFormat commented 5 years ago

Could you please update the issue and specify your Laravel version?

Laravel 5.7

intruder897 commented 5 years ago

I had a same problem with php 7.2.13 and laravel 5.7

I figured out that the problem is in Attributable trait.

There is a check in getEntityAttribute: preg_match('/^raw(\w+)object$/i', $key). getEntityAttribute will return Illuminate\Support\Collection if check fails and Rinvex\Attributes\Support\ValueCollection if pass.

So to be able to add multiple values to color attribute collection just use this: $product->rawcolorobject->add([red, green]).

I don't know is this a bug or so conceived, i'm new to Laravel and laravel-attributes

dellow commented 5 years ago

Having the same issue as @intruder897. If you remove the check in getEntityAttribute() everything works as expected.

Please can we get a fix for this? Package seems unusable right now as the ->add method doesn't work.

Thanks.

sidorenkoda commented 5 years ago

You must add is_collection' => 1 to create of attribute and use $product->getRelationValue('size')->add($size); for add

app('rinvex.attributes.attribute')->create([ 'slug' => 'size', 'type' => 'varchar', 'name' => 'integer', 'entities' => [Product::class], 'is_collection' => 1 ]);

for ($size = 42; $size < 65; $size = $size + 2) {
    $product->getRelationValue('size')->add($size);
}

If you add 'is_collection' => 1 type automatic change to "varchar"

Omranic commented 3 years ago

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! 🙂