【问题标题】:Can't update a translatable model in Laravel无法在 Laravel 中更新可翻译模型
【发布时间】:2021-01-18 21:14:00
【问题描述】:

我正在使用https://github.com/Astrotomic/laravel-translatable 使我的模型可翻译。

虽然我正在尝试使用以下方法更新模型以更新模式及其相关的翻译内容。

$product = $this->repository->update($input, $uuid);

   // output of dd($input);

  "uuid" => "44b26fb0-04b9-11eb-981a-6b01570d58ed"
  "_method" => "PUT"
  "en-us" => array:5 [▶]
  "ar-eg" => array:5 [▶]
  "account_uuid" => "a1c23ce0-04b7-11eb-b060-7faa78f23afd"
  "type" => "tour"
  "iso_4217" => "EGP"
  "status" => "pending"

我收到了这个错误:

Column not found: 1054 Unknown column 'id' in 'where clause'

考虑到模型主键是“uuid”而不是“id”,因此根据下面的模型设置,外键是“product_uuid”而不是“product_id”。

class Product extends Model implements Transformable, TranslatableContract
{
    public $incrementing = false;
    public $keyType = 'string';
    public $timestamps = true;
    protected $primaryKey = 'uuid';

    public $translatedAttributes = [
        'name',
        'quantity_label_snigular',
        'quantity_label_plural',
        'brief_description',
        'long_description',
        'terms',
        'meta_keywords',
    ];

    public $translationForeignKey = 'product_uuid';
    public $localeKey = 'uuid';    
    public $useTranslationFallback = true;    
}

我按照此链接https://docs.astrotomic.info/laravel-translatable/usage/forms#saving-the-request 上的确切文档进行操作,尽管我无法追踪该错误。

【问题讨论】:

    标签: laravel laravel-translatable


    【解决方案1】:

    我发现问题出在 productTranslation 模型的设置中。

    “product_translations”表主键是“product_uuid”和“locale”的组合。

    所以在“ProductTranslation”模型中,我将主键定义为一个数组

    protected $primaryKey = ['product_uuid', 'locale'];
    

    并扩展这些方法以根据此答案从数组而不是字符串中获取主键https://stackoverflow.com/a/37076437

        /**
         * Set the keys for a save update query.
         *
         * @param  \Illuminate\Database\Eloquent\Builder  $query
         * @return \Illuminate\Database\Eloquent\Builder
         */
        protected function setKeysForSaveQuery(Builder $query)
        {
            $keys = $this->getKeyName();
            if(!is_array($keys)){
                return parent::setKeysForSaveQuery($query);
            }
    
            foreach($keys as $keyName){
                $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
            }
    
            return $query;
        }
    
        /**
         * Get the primary key value for a save query.
         *
         * @param mixed $keyName
         * @return mixed
         */
        protected function getKeyForSaveQuery($keyName = null)
        {
            if(is_null($keyName)){
                $keyName = $this->getKeyName();
            }
    
            if (isset($this->original[$keyName])) {
                return $this->original[$keyName];
            }
    
            return $this->getAttribute($keyName);
        }
    

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 2021-08-17
      • 2021-11-30
      • 2021-11-19
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 2019-06-13
      • 2015-02-16
      相关资源
      最近更新 更多