简体   繁体   English

laravel-translatable:将现有文本列转换为可翻译

[英]laravel-translatable: converting existing text column to translatable

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)我发现当我将列名添加到受保护的可翻译数组时,我不再能够像以前那样访问它($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我想检索文本并将其替换为 json

when I log $this i can see my object and the correct key: value pairs.当我登录 $this 时,我可以看到我的 object 和正确的键:值对。 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

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资源: https://github.com/spatie/laravel-translatable#getting-all-translations-in-one-go

it because the column value still in text.这是因为列值仍在文本中。 if you already use trait HasTranslations in your model and want to access the column's value that still not convert in json, just simply use如果您已经在 model 中使用 trait HasTranslations 并且想要访问在 json 中仍未转换的列值,只需使用

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

method.方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM