How do I receive URL parameters in a custom page?

Sorry, this is a really simple question, but I have been struggling with it. I've created a custom page: <?php namespace App\Filament\Pages; use Filament\Pages\Page; class VetProfile extends Page { protected string $view = 'filament.pages.vet-profile'; public function mount(): void { } } I want to be able to pass the member id to this page so it knows which record to display and handle, either like this: /vet-profile/34839439 or if I have to: /vet-profile?memberId=34839439 How do I do this?
12 Replies
Finn
Finn2w ago
Livewire does a lot of magic behind the scenes, which makes passing data between requests a little abnormal. You can add a property to the page in order to pass through a query parameter here, for example ?someVariable=1, but I'm aware that the query parameter method isn't your preferred option.
class ViewArticle extends ViewRecord
{
protected static string $resource = ArticleResource::class;

#[Url]
public ?int $someVariable = null;
class ViewArticle extends ViewRecord
{
protected static string $resource = ArticleResource::class;

#[Url]
public ?int $someVariable = null;
keiron
keironOP2w ago
Thanks, so I changed my file to: <?php namespace App\Filament\Pages; use App\Filament\Resources\Members\MemberResource; use Filament\Resources\Pages\ViewRecord; use Livewire\Attributes\Url; class VetProfile extends ViewRecord { protected static string $resource = MemberResource::class; #[Url] public ?int $memberId = null; protected string $view = 'filament.pages.vet-profile'; } I then tried to access: /vet-profile?memberId=123 but I get a 404 error
LeandroFerreira
protected static ?string $slug = 'vet-profile/{id?}';

public function mount(?string $id): void
{
//...
}
protected static ?string $slug = 'vet-profile/{id?}';

public function mount(?string $id): void
{
//...
}
keiron
keironOP2w ago
Is this to append to my change or to replace the approach somehow?
LeandroFerreira
if it is a Member, use InteractsWithRecord check the link
keiron
keironOP2w ago
Thanks, is there a working example anywhere that I can have a look at? or a v4 tutorial
LeandroFerreira
didn't it work?
keiron
keironOP2w ago
I think my confusion has come from creating a standalone Page vs a Resource page. I already created a standalone Page that gets all members that require vetting. It shows them in a table. Would you suggest this should be a Resource Page instead?
keiron
keironOP2w ago
Thank you for your patience with me on this. I have stripped back my code to the following, which is now able to access userId from the query parameters and also render a form: <?php namespace App\Filament\Pages; use Filament\Forms\Components\Checkbox; use Filament\Forms\Concerns\InteractsWithForms; use Filament\Forms\Contracts\HasForms; use Filament\Pages\Page; use Filament\Schemas\Schema; use Livewire\Attributes\Url; class VetProfile extends Page implements HasForms { use InteractsWithForms; protected string $view = 'filament.pages.vet-profile'; #[Url] public ?int $userId = null; public function mount(): void { info($this->userId); } public function form(Schema $schema): Schema { return $schema ->components([ Checkbox::make('is_weird') ->label('Exclude from marketing materials') ->columnSpanFull(), ]); } } <x-filament::page> {{ $this->form }} </x-filament::page> So now I am wondering if I am doing this correctly. With Resource pages the submit button etc is all added for me. However with this approach, it looks like I need to add it myself somehow. Is this still the correct approach?
LeandroFerreira
yes, you can do this, but if you want to render a form you should follow this section

Did you find this page helpful?