#Fetch relational data in custom page

7 messages · Page 1 of 1 (latest)

shadow sluice
#

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(); }

shadow sluice
#

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?

lyric grotto
#

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.

shadow sluice
#

That explains it... By a group, do you mean a fieldset? can you give me an example of how you do that?

lyric grotto
#
Section::make(‘blah’)
    ->relationship(‘relation’)
    ->schema([])