Closed YoshiMannaert closed 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
]);
@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
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
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:
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:
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'];