laravel-translatable: converting existing text column to translatable

774 Views Asked by At

i'm trying to convert and existing text column to translatable. I find that when i add the column name to the the protected translatable array i am no longer able to access it as i did before ($model->key)

I assume that this is because its looked for a translation but can't find one. Is there a way for me to return to contents of the column? I want to retrieve the text and and replace it with a json

when I log $this i can see my object and the correct key: value pairs. Any attempt to access it or convert it to array causes the value to disappear completely

$array = json_decode(json_encode($this), true);
$object = json_decode(json_encode($this), false);

error_log('$this     '.print_r($this,true)); // includes the key 'myKey' with correct value

error_log('$array     '.print_r($array['mykey'],true)); // empty
error_log('$object     '.print_r($object->mykey,true)); // empty
2

There are 2 best solutions below

0
On

You can use this method if you want to get all translated values of a particular column as an array.

public function update(ModelName $modelItem)
{
    return $modelItem->getTranslations('column_name');
}

//result 
[
   'en' => 'test',
   'tr' => 'deneme',
]

Resource: https://github.com/spatie/laravel-translatable#getting-all-translations-in-one-go

1
On

if you want to get the content that still not store as json translation, you can use this eloquent method.

$model->getRawOriginal('your translation's column name');

it will get your column value.