JSON return when use api resource #340
-
Hello, I'm using the translatable to get data throw api, I'm using Laravel resource for making json from my model
But I'm getting JSON for the translated field! I need a language I select from the ApiController to the local lang
I want to get the translated name! not the json many thanks for @anibalealvarezs for his help in past errors =) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello. Don't worry, man. I've been working intensively with the package past months, having almost the same issues. That's why it's easy for me to address to them now. Previously, you already added a mutator for the If it's the same model, that's why you're getting the full JSON instead of the translated name. protected $appends = ['raw_name']; May it be the same model of a different one, in order to get the trasnlated name you have to mutate the field properly, like stated in the issue I attached before: #320 (comment) This way, your model will include two attributes:
{
"id": 168,
"name":"\ا\ل\د\ك\ت\و\ر \س\و\ر\ا\ن \ا\ح\م\د \ا\ل\ش\و\ا\ن\ي*\",
"notes": "asdfasdf",
"is_special": 0,
"stars": 5,
"image_url": "http://bookupdoctor.test/storage/223/60dc9b05e3c85_دكتور.png",
"raw_name":"{\"en\":\"\ا\ل\د\ك\ت\و\ر \س\و\ر\ا\ن \ا\ح\م\د \ا\ل\ش\و\ا\ن\ي*\"}"
}, |
Beta Was this translation helpful? Give feedback.
Hello.
Don't worry, man. I've been working intensively with the package past months, having almost the same issues. That's why it's easy for me to address to them now.
Previously, you already added a mutator for the
name
field in order to get the original value when there was no translation: #337 (reply in thread)If it's the same model, that's why you're getting the full JSON instead of the translated name.
I would rename that "raw" response to
getRawNameAttribute
and append de attribute to the modelMay it be the same model of a different one, in order to get the trasnlated name you have to mutate the field properly, like stated in the issue I attached…