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
433 stars 104 forks source link

I can't attach attributes #89

Closed livevasiliy closed 2 years ago

livevasiliy commented 5 years ago

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: update products set width = 123, products.updated_at = 2019-05-09 22:30:34 where id = 1)

Please help. Thanks

webaction commented 5 years ago

I am also having the same problem. Could this have something to do with the save events?

tungdocrowdco commented 5 years ago

Does any one find the way to fix it?

christiancannata commented 5 years ago

I am also having the same problem, any solutions?

Lumrenion commented 4 years ago

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)?

Lumrenion commented 4 years ago

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.

mucan54 commented 3 years ago

I have same issue

mucan54 commented 3 years ago

I solved my problem in attribute_entity table I added my entity type like App\Models\Product, not like ['App\Models\Product']

tungtreviet commented 3 years ago

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,
        ]);
    }
}
Omranic commented 2 years ago

Thank you for your patience, all the above questions are actually answered in the docs. I know it's long and not easy docs, but that's the nature of EAV anyway, you need to understand it well first before using it. Please refer to the docs.