Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.24k stars 156 forks source link

Translation not filled using attach on translatable Morphpivot relation #213

Closed sornss closed 3 years ago

sornss commented 3 years ago

Hello gentlemen,

I have tried to translate a morphpivot relation (Categorizable, CategorizableTranslation) with the following schema, structure:

Schema::create('categories', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('title')->nullable()
});

Schema::create('categorizables', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->nullableMorphs('categorizable');
    $table->bigInteger('category_id')->nullable();
});

Schema::create('categorizable_translations', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('categorizable_id');
    $table->string('extra_title')->nullable();
});

Schema::create('products', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name')->nullable()
});

The related classes:

class Category extends Model 
{
    public $fillable = [ 'title' ];

}

class Categorizable extends Morphpivot
{
    use Translatable;

    protected $incrementing = true;

    public $translatedAttributes = [ 'extra_title' ];

    public $fillable = [ 'categorizable_id', 'categorizable_type', 'category_id' ];

    public function categorizable()
    {
        return $this->morphTo();
    }

}

class CategorizableTranslation extends Model
{
    public $fillable = [ 'extra_title', 'locale', 'categorizable_id' ];

    public categorizable() 
    {
        return $this->belongsTo(Categorizable::class);
    }

}

class Product extends Model
{
    public $fillable = [ 'name' ];

    public function categories()
    {
        return $this->morphToMany(Category::class, 'categorizable', 'categorizables')
                    ->using(Categorizable::class)
                    ->withPivot(['extra_title', 'categorizable_id', 'categorizable_type']);
    }

    public categorizable() 
    {
        return $this->morphMany(Categorizable::class);
    }

}

And I tried to attach an existing category to a product via the Product's categories as below:

$aProduct->categories()->attach($anExistingCategory, [ 'extra_title' => "An extra title" ]);

As result, the table categorizables is filled correctly but not the translation table categorizable_translations .

Any help and/or concerns please for the translation to be inserted as well in the translation table categorizable_translations ?

Gummibeer commented 3 years ago

This package was never tried with pivot tables as they behave totally differently. I would recommend you to use a real model if you want to use more complex logic on the intermediate table/model.

sornss commented 3 years ago

Well when I tried to use create on the Product's categorizable like below, the translation table was filled correctly:

$aProduct->categorizable()->create( [ "extra_title" => "An extra title", "category_id" => $anExistingCategory->id ] );

It is sufficient enough for me to proceed with the above approach. But I do prefer using attach via the Product's categories, and it looks better, no?

Please shed some light on this, if possible, why the translation is not loaded with attach.

Thank you.

Gummibeer commented 3 years ago

Hey, I've source-dived it and attach() doesn't use fill() - the attributes take a long path:

So the attributes are set as raw attributes which just assigns the array to the $attributes property.

The create() method uses a different path:

Gummibeer commented 3 years ago

So I will close this issue as there's nothing we could do here.