Passing data to custom pages

Hi everyone! I need to build something like a custom resource, but using pages. It's a little more complicated, but imagine a model User that I display in a table first, with clickable entries that lead me to the corresponding EditPage and the prefilled form. So basically regular resource behavior. I managed to build the table and have the rows clickable with ->recordUrl(fn(User $record) => route('filament.admin.pages.user-edit', ['record' => $record->id])) but I struggle with the form. So my questions are: - Is that the correct way of making the row clickable and passing the record? - How do I set up the Edit Page correctly? Meaning getting the passed $record data and prefilling the form? My current approach for the EditPage (based on ChatGPT) is:
class RegistrationReviewEdit extends Page implements HasForms
{
use InteractsWithForms;
public ?User $record = null;

public ?array $data = [];
protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.pages.user-edit';

public function mount(User $record): void
{
$this->record = User->find($record);
$this->form->fill([
'first_name' => $this->record->first_name,
'last_name' => $this->record->last_name,
'email' => $this->record->email,
]);
}

public function form(Form $form): Form
{
return $form
->schema([
...
]);
}
}
class RegistrationReviewEdit extends Page implements HasForms
{
use InteractsWithForms;
public ?User $record = null;

public ?array $data = [];
protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.pages.user-edit';

public function mount(User $record): void
{
$this->record = User->find($record);
$this->form->fill([
'first_name' => $this->record->first_name,
'last_name' => $this->record->last_name,
'email' => $this->record->email,
]);
}

public function form(Form $form): Form
{
return $form
->schema([
...
]);
}
}
This doesn't work though. I think I need to update the route so it accepts parameters, php artisan route:list tells me, that unlike the regular resources, the route for my Edit Page doesn't accept a parameter. But I also don't know how to get this parameter inside the page. I'm still kinda new to Laravel and Filament and I feel like I'm missing some basic knowledge, that I struggle to find in the docs. Where can I find these info in docs? Are there any tutorials to this specific topic?
2 Replies
Dennis Koch
Dennis Koch6d ago
This is a syntax error and also not needed. You already have a user instance: $this->record = User->find($record);, but apart from that you are good. You can use ->formFill($this->record->toArray()) to save some lines.
tells me, that unlike the regular resources, the route for my Edit Page doesn't accept a parameter
You probably just need to set the $slug property.
sparmin
sparminOP6d ago
I've added public static string|null $slug = 'user-edit'; But that didnt have any effect. The registered route still has no parameter and dd($record) is empty and as long as this is the case, I can't even debug if my ->mount() function is correct

Did you find this page helpful?