I am using https://github.com/Astrotomic/laravel-translatable to make my models translatable.
While I am trying to update the model using below method to update the mode along with its associated translated content.
$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"
I got this error:
Column not found: 1054 Unknown column 'id' in 'where clause'
considering that the model primary key is "uuid" not "id" hence the foreign key is "product_uuid" not "product_id" as per below model setup.
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;
}
I followed the exact documentation at this link https://docs.astrotomic.info/laravel-translatable/usage/forms#saving-the-request, although I am not able to trace that error.
I figured out that The issue was in the setup of the productTranslation model.
the 'product_translations' table primary key is a composite of 'product_uuid' and the 'locale'.
so in the 'ProductTranslation' model I defined the primary key as an array
protected $primaryKey = ['product_uuid', 'locale'];
and extended those methods to fetch the primary keys from array instead of string as per this answer 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);
}