Astrotomic可平移-枢轴柱- Laravel 9

mmvthczy  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(120)

转换透视列时遇到问题。
我已经尝试了一整天添加这个翻译,但它仍然不工作。
我正在使用该软件包:https://github.com/Astrotomic/laravel-translatable
普通表可以正常工作,但透视表不行。
我的代码(命名很快,因为代码将工作,我要重构它):

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。

另外,当我手动向数据库添加事务时,它也不显示它。
我将非常感谢帮助这个翻译。

xqkwcwgp

xqkwcwgp1#

好吧,我把它改成这样了,只不过我得传身份证而不是子弹。

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

相关问题