Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get array for all translation with model? example: all()->getTranslationsArray() #218

Closed
nekooee opened this issue Apr 8, 2021 · 8 comments
Assignees

Comments

@nekooee
Copy link

nekooee commented Apr 8, 2021

how to get array for all translation with model? example: all()->getTranslationsArray()

I need to get a translation for my post model in the array.
like this:

{
    "id": 1,
    "slug": "subject-1",
    "title": "subject 1",
    "content": "content 1",
    "translations": [
        {
		"en":[
            "title": "subject 1",           
            "content": "content 1"
			],
		"nl":[
            "title": "onderwerp 1",           
            "content": "inhoud 1"
			],
        }       
    ]
},
{
    "id": 2,
    "slug": "subject-2",
    "title": "subject 2",
    "content": "content 2",
    "translations": [
        {
		"en":[
            "title": "subject 2",           
            "content": "content 2"
			],
		"nl":[
            "title": "onderwerp 2",           
            "content": "inhoud 2"
			],
        }       
    ]
}

please help me.

@elnurxf
Copy link

elnurxf commented Apr 19, 2021

+1

@Gummibeer
Copy link
Member

Gummibeer commented Apr 19, 2021

Totally depends on the way you are generating that response. In general $model->translations is a default Laravel relationship that contains all translation models - so you can add them to your JSON response however you want. I recommend an eloquent API resource class.

@Gummibeer Gummibeer self-assigned this Apr 19, 2021
@elnurxf
Copy link

elnurxf commented Apr 19, 2021

Thank you for your response. The thing is that we want to translations will come with keyBy('locale')

@Gummibeer
Copy link
Member

You can do this in your API resource. You get a collection of translation models - so a quick way would be something like:

$model->translations->keyBy('locale')->map->only('title', 'content');

The code isn't tested but should show one way to do it - it's by far not the best/cleanest way to do, but should work. 😉

@elnurxf
Copy link

elnurxf commented Apr 19, 2021

That's works if I set a separate variable, unfortunately translations attribute can't be overrided.

@Gummibeer
Copy link
Member

It doesn't have to be overridden!?

Your full JSON would be something like:

return response()->json([
    'id' => $model->id,
    'slug' => $model->slug,
    'translations' => $model->translations->keyBy('locale')->map->only('title', 'content'),
]);

@nekooee
Copy link
Author

nekooee commented Apr 19, 2021

I used this method in the model before you answered. I do not know how optimal it is. Although it works properly:

 protected $appends = [ 'translation_arrays'];
 public function getTranslationArraysAttribute()
    {
        $title = $this->translations()->pluck('title', 'locale');
        $content = $this->translations()->pluck('content', 'locale');
        return ['title' => $title, 'content' => $content];
    }

@github-actions
Copy link
Contributor

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants