简体   繁体   English

Astrotomic Translatable - pivot 专栏 - Laravel 9

[英]Astrotomic Translatable - pivot column - Laravel 9

I'm having trouble translating a pivot column.我在翻译 pivot 列时遇到问题。

I've been trying all day to add this translation but it still doesn't work.我整天都在尝试添加这个翻译,但它仍然不起作用。

I'm using the package: https://github.com/Astrotomic/laravel-translatable我正在使用 package: https://github.com/Astrotomic/laravel-translatable

Plain tables work fine, but pivot doesn't.普通表工作正常,但 pivot 不行。

My code (the naming was quick, as the code will work, I'm going to refactor it):我的代码(命名很快,因为代码可以工作,我将重构它):

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;
                            }
                }
            }

The structure of the attributes is:属性的结构是:

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

Unfortunately pivot->translate() always returns null.不幸的是 pivot->translate() 总是返回 null。

在此处输入图像描述

Also, when I manually add transactions to the database, it does not display it.另外,当我手动将事务添加到数据库时,它不会显示它。

I will be very grateful for help with this translation.我将非常感谢您对此翻译的帮助。

Okay, I fixed it like this, only I have to pass id instead of slugs.好的,我是这样修复的,只是我必须传递 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