Astrotomic / laravel-translatable

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

when I use pivot model, get error: "getTranslationRelationKey(): Return value must be of type string, null returned" #260

Closed nekooee closed 2 years ago

nekooee commented 2 years ago

I wrote my code according to this tutorial:

https://docs.astrotomic.info/laravel-translatable/usage/pivot-model

my pivot model:

class AttributeProduct extends Pivot implements TranslatableContract
{

    use Translatable;

    public $incrementing = true;
    public $fillable = [ 'product_id', 'attribute_id'];

    public $translatedAttributes = ['value'];

    final public function value()
    {
        return $this->BelongsTo(AttributeValue::class, 'attribute_value_id', 'id');
    }

}

and translation model for pivot model:

class AttributeProductTranslation extends Model
{
    public $timestamps = false;
    protected $fillable = ['value','locale','attribute_product_id'];
}

I tried to save the translation in the controller:

  $product->attributes()->attach($attribute_id);
  $attributeProduct = AttributeProduct::where(['product_id' => $product_id, 'attribute_id' => $attribute_id])->first();

  $attributeProduct->translate('en')->value = 'Value in English';
  $attributeProduct->save();

But I get this error:

message: "App\Models\AttributeProduct::getTranslationRelationKey(): Return value must be of type string, null returned"

please help me. I really need to know the solution to this problem.

nekooee commented 2 years ago

I added protected $translationForeignKey = 'attribute_product_id'; to "AttributeProduct" pivot model. now work fine.

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days