Closed livevasiliy closed 2 years ago
I am also having the same problem. Could this have something to do with the save events?
Does any one find the way to fix it?
I am also having the same problem, any solutions?
It would be great if this was possible. My specific use case is:
I tried appending it with $model->attributes()->setOffset('attributeName', $attributeCreatedWithAppCreate)
.
When calling $model->getRelationValue('attributeName')->add($value)
, I get "calling add on null", because the relation is still missing for the attribute created on the fly.
Do I need to create the Relation Object (HasOne, HasMany) manually and add it with method $model->setRelation('attributeName', $manuallyCreatedRelation)
?
I think I found a solution. To create a new attribute for a model and attach it to an object:
$createdAttribute = app('rinvex.attributes.attribute')->create([...]);
$this->attributes()->offsetSet($attributeSlug, $createdAttribute);
app(\Rinvex\Attributes\Support\RelationBuilder::class)->build($this)
note: $this is the model itself.
If you have multiple objects next to the one where you add the attribute, you have to refresh their attributes (code is also in context of the model):
self::$entityAttributes = null;
$this->entityAttributeRelationsBooted = false;
$this->bootIfNotBooted();
That's it, a new attribute has been added to the model on the fly, you can set the value and save the model.
I have same issue
I solved my problem in attribute_entity table I added my entity type like App\Models\Product, not like ['App\Models\Product']
Just add type register in AppServiceProvider or your CustomServiceProvider
<?php
namespace App\Providers;
use App\Models\Product;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Lorisleiva\Actions\Facades\Actions;
use Rinvex\Attributes\Models\Attribute;
use Illuminate\Database\Eloquent\Builder;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Add to boot method
$this->registerEavTypes();
$this->registerEavModel();
}
// Regis your models
private function registerEavModel()
{
app('rinvex.attributes.entities')->push(Product::class);
}
// Regis which types you want to use here
private function registerEavTypes()
{
Attribute::typeMap([
'varchar' => \Rinvex\Attributes\Models\Type\Varchar::class,
'text' => \Rinvex\Attributes\Models\Type\Text::class,
'boolean' => \Rinvex\Attributes\Models\Type\Boolean::class,
'integer' => \Rinvex\Attributes\Models\Type\Integer::class,
'datetime' => \Rinvex\Attributes\Models\Type\Datetime::class,
]);
}
}
Hello !
I can't attach created attributes on my model. I made attribute , use Tinker built in Laravel Framework:
app('rinvex.attributes.attribute')->create([
'slug' => 'width',
'type' => 'varchar',
'name' => 'Product width',
'entities' => ['\App\Product'],
]);
This code work OK, i getting new record in my database. Next i create new record for my model \App\Product, I'm write in Tinker like this code:use \App\Product;
$product = Product::create([
'name' => 'Goods 1',
'price' => 123.45
]);
$product->save();
I get new record in my table. Now I try attatch attribute to my created early records. Like this:
use \App\Product;
$product = Product::find(1);
$product->width = "123";
=>"123"
<--- This i get message from Tinker$product->save();
<--- This I get error message like this Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'width' in 'field list' (SQL: updateproducts
setwidth
= 123,products
.updated_at
= 2019-05-09 22:30:34 whereid
= 1)Please help. Thanks