Fetch relational data in custom page
How can I fetch the relational data in personable.full_name?
public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('title')
->autofocus()
->required(),
TextInput::make('personable.full_name')
->required(),
])
->statePath('data')
->model($this->ticket);
}
public Ticket $ticket;
public function mount(int | string $record): void
{
$this->ticket = Ticket::findOrFail($record);
$this->form->fill($this->ticket->attributesToArray());
}
User.php model:
public function tickets(): MorphMany
{
return $this->morphMany(Ticket::class, 'personable');
}
Ticket.php model:
public function personable(): MorphTo
{
return $this->morphTo();
}
4 Replies
When I’m using the same code with an info list personable.full_name is working. But why can’t I populate the field in edit mode in the form? What am I doing wrong?
Dot syntax doesn’t eager load the relationship in forms like it does for tables and infolists.
Some fields like select support a relationship, but in the context of a form it doesn’t always work. Ie when creating a record there is no relationship yet.
You can wrap the field in a Group layout component and set a relationship on that though.
That explains it... By a group, do you mean a fieldset? can you give me an example of how you do that?
All the layout components support relationship. And there is a Group component which isn’t documented for some reason.