Rick Doetinchem
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();
}
8 replies
Custom page with resource record error
I have a custom page:
public static function getPages(): array
{
return [
'ticketWithComments' => Pages\TicketWithComments::route('/{ticketId}/ticketwithcomments'),
…
];
}
And I try to pickup the ticketId and fill the form with the following code:
public function mount($ticketId): void
{
$this->ticket = Ticket::findOrFail($ticketId);
$this->form->fill($this->ticket->attributesToArray());
}
But I’m getting error: Typed property App\Filament\App\Resources\TicketResource\Pages\TicketWithComments::$ticket must not be accessed before initialization
What am I doing wrong?21 replies