0

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
Ben Kemp
  • 1,501
  • 2
  • 7
  • 9

2 Answers2

0

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

0

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.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 27 '22 at 15:38