Translation hasMany relation: how to populate form and field by locale
I have to a model: Category, it has a relation CategoryI10n which has a field: locale.
In the Category model I defined the relation:
I iterate through the available locales, create tabs, and text fields:
TextInput::make("name_$locale")
Then in the CreateCategory.php I implemented a custom handleRecordCreation function, which creates the necessary records in the relation.
But I have no clue, how to display only one translation record corresponds to the current record:
- if I add TextColumn::make("translations.name")
, it displays all the 3 available translations.
- I tried to write a __get()
function in Category model, if the property is name_en, name_fr it query the relation and give back the correct value. It works well in the tinker, but for some reason the TextColumn::make("name_en") does not call the __get()
method.
Can you help me here how would you handle this problem?
Thank you in advance!8 Replies
In your case it would be very helpful if you had a demo repo. This way it is very difficult to understand what it should do and what it is currently doing
Here is the migration file:
I want to display here only one related record which is corresponding to the current local
I also have no clue, how to populate the form when I am editing a record
I wrote a getter function in the Category model:
I expocted when I use the 'name_en' in the TextColumn::make function, it will call this function (as name_en is not a data field in the schema)
Maybe something like this:
In the edit page of the respective resource? Otherwise via ->fillForm() if you call it via an action?
Hello CyQuer! Thank you very much for your help! After re-reading the documentation, finally I managed to implement as I wanted.
the biggest problem was I did not know exactly what I wanted
but the ListCategories -> getTabs() function was the solution