Closed batFormat closed 3 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 ?
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())
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.
Could you please update the issue and specify your Laravel version?
Could you please update the issue and specify your Laravel version?
Laravel 5.7
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
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.
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"
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! 🙂
Method Illuminate\Support\Collection::add does not exist.
Sample code,