簡體   English   中英

Astrotomic Translatable - pivot 專欄 - Laravel 9

[英]Astrotomic Translatable - pivot column - Laravel 9

我在翻譯 pivot 列時遇到問題。

我整天都在嘗試添加這個翻譯,但它仍然不起作用。

我正在使用 package: https://github.com/Astrotomic/laravel-translatable

普通表工作正常,但 pivot 不行。

我的代碼(命名很快,因為代碼可以工作,我將重構它):

class Offer extends Model implements TranslatableContract
                               use HasFactory, Translatable;
                         public array $translatedAttributes = [
                                'name',
                                'description'
                            ];
                         public function attributes(): BelongsToMany
                            {
                                return $this->belongsToMany(Attribute::class, 'attribute_offer')->using(AttributeOffer::class)->withTimestamps();
                            }
                    
            class Attribute extends Model implements TranslatableContract
                        {
                            use HasFactory, Translatable;
                        
                        
                            public array $translatedAttributes = [
                                'name',
                            ];
                      public function values(): BelongsToMany
                        {
                            return $this->belongsToMany(Offer::class, 'attribute_offer', 'attribute_id', 'offer_id')->using(AttributeOffer::class);
                        }
                    
            class AttributeOffer extends Pivot implements TranslatableContract
                    {
                        use Translatable;
                    
                    
                        public $incrementing = true;
                    
                        public array $translatedAttributes = [
                            'content',
                        ];
                    
                    
                        protected $fillable = [
                            'attribute_id',
                            'offer_id',
                        ];
                    }
    class AttributeOfferTranslation extends Model
        {
            protected $table = 'attribute_offer_translations';
        
            public $timestamps = false;
        
            protected $fillable = [
                'content',
            ];
        }
  class OfferController extends Controller
                {
                  private function updateAttributeValues($offer, $attributes)
                    {
                    foreach ($attributes as $slug => $values) {
                            $pivot = $offer->attributes()->whereSlug($slug)->first()->pivot;
                            foreach ($values as $locale => $value) {
                                $pivot->translate($locale)->content = $value;
                            }
                }
            }

屬性的結構是:

[
    'test' =>[
    'en' =>  'test',
    'es' =>  'test',
    'de' =>  'test',
     ],
    'test2' =>[
    'en'=> 'test',
    'es'=> 'test',
    'de'  => 'test',
     ],
]

不幸的是 pivot->translate() 總是返回 null。

在此處輸入圖像描述

另外,當我手動將事務添加到數據庫時,它不會顯示它。

我將非常感謝您對此翻譯的幫助。

好的,我是這樣修復的,只是我必須傳遞 id 而不是 slugs。

class Offer extends Model implements TranslatableContract
{
...
    public function attributeValues(): HasMany
    {
        return $this->hasMany(AttributeOffer::class);
    }
}
class AttributeOffer extends Pivot implements TranslatableContract
{
...
    protected $translationForeignKey = 'attribute_offer_id';
...
}
private function updateAttributeValues($offer, $attributes)
  {
        foreach ($attributes as $id => $values) {
            $offer->attributeValues()->whereAttributeId($id)->first()->update($values);
        }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM