Closed ahmedsaber111 closed 4 years ago
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);
}
While I am trying to update the model using below method to update the mode along with its associated translated content.
I got this error:
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.
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.