Simple question about nested models with Form

Hello, This is my first time using Filament, so please be gentle with me. I've tried to find a solution on-line, including asking AI, but I cannot figure out what I am doing wrong. I got three models: Child, Parent, and Grandparent. Child belongs to Parent, Parent belongs to Grandparent. I would like to display, on the Child view page, values from both Parent and Grandparent. In the form builder I have tried to access their values using dot notation, so it looks like this:
Section::make('Child')
->schema([
TextInput::make('name'),
TextInput::make('parent.name')->label('Parent'),
TextInput::make('parent.grandparent.name')->label('Grandparent'),
])
Section::make('Child')
->schema([
TextInput::make('name'),
TextInput::make('parent.name')->label('Parent'),
TextInput::make('parent.grandparent.name')->label('Grandparent'),
])
Unfortunately, neither parent.name nor parent.grandparent.name values are being populated. I have overloaded ViewRecord::mount() to check if record is populated correctly - it is. I have tried to use ->state(fn (Child $record) => $record->parent->name) to set the value, but this throws an exception about accessing uninitialised $container. Is there a way to build a view page that does not look like a form? I have some resources (or part of resources) I would like to view and never edit. Any help/pointers will be appreciated. Kind regards, Marek
6 Replies
Bruno Pereira
Bruno Pereira2w ago
Have you defined the relations in the Model correctly?
Matthew
Matthew2w ago
Yes...infolist.
Marek
MarekOP2w ago
Yes, relationships in model are set correctly, I can render data without issues using the Placeholder.
Bruno Pereira
Bruno Pereira2w ago
you have to define the field as a relationship if I'm not mistaken
Marek
MarekOP2w ago
I have not tried this, but I have found Placeholder that works better for me.

Did you find this page helpful?