Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.23k stars 155 forks source link

How to access base model data #305

Closed YoshiMannaert closed 1 year ago

YoshiMannaert commented 1 year ago

Hi, I have a Page model which is translatable and I'm currently working on the edit page for the pages. I'm sending the data of the Page there with the translations of the selected locale which is working fine. However, I still need to use some data from the base Page model, e.g. the id. The problem is that it returns the id of the translation, instead of the Page model. I removed the id column from the translations table, to see if maybe that would fix it but now it just returns null.

Is there a way that I can add the translations but also still access the default/parent model?

In my controller I have this:

public function show($id, $locale)
    {
        $page = Page::findOrFail($id);
        return Inertia::render('Pages/Show', [
            'page' => new SinglePageResource($page->translate($locale)),
            'locale' => $locale,
            'parent_id' => $id
        ]);
    }

I'm now passing the id of the base Page model, but preferably I could access the base model data in my resources as well

SinglePageResource:

public function toArray($request)
    {
        return [
            'id' => $this->id,    // Maybe something like $this->parent->id
            'title' => $this->title,
            'sub_title' => $this->sub_title,
            'body' => $this->body,
            'slug' => $this->slug,
            'created_at' => $this->created_at->format('d/m/Y'),
            'updated_at' => $this->updated_at->format('d/m/Y'),
            'status' => $this->status_id,
        ];
    }

And in my models I have these attributes:

Page model: public $translatedAttributes = ['title', 'sub_title', 'body', 'slug']; PageTranslation model: protected $fillable = ['title', 'sub_title', 'body', 'slug'];

ronrun commented 1 year ago

I haven't used Inertia. But maybe I can see some problem from your code. I see you first get $page
$page = Page::findOrFail($id);
This is actually the Page model and pages table. Then you use
$page->translate($locale) So, you pass the translation model to Inertia, of course you don't see data from page model.

I think you should use

$page = Page::findOrFail($id)->withTranslation($locale);
return Inertia::render('Pages/Show', [
    'page' => new SinglePageResource($page),
    'locale' => $locale,
    'parent_id' => $id
]);
YoshiMannaert commented 1 year ago

@ronrun Hm, if I use ->withTranslation($locale) I get an error Property [id] does not exist on the Eloquent builder instance. (if I remove the ID from the resource it just returns the same error for the next column)

If I use ->translate($locale) then it does work but again, it returns the translation ID instead of the page ID

github-actions[bot] commented 1 year ago

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