F
Filamentā€¢7mo ago
TK

How to route to a page with a given filter?

Currently I have a resource with a relation resource. Those relation resource records should get a url to another page with there given property as a filter. So, in my ExampleRelationManager I have this:
return $table
->...
->actions([
ActionGroup::make([
Action::make('...')
->...
->action(
fn($record) => redirect()->to(
ListOtherResourceFilament::getUrl([
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
])
)
)
,
]),
]);
return $table
->...
->actions([
ActionGroup::make([
Action::make('...')
->...
->action(
fn($record) => redirect()->to(
ListOtherResourceFilament::getUrl([
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
])
)
)
,
]),
]);
This is not working. I assume there is a correct way to give filters to the ::getUrl() method, just like given a route parameter. For the record, the URL to end up with, should be: https://example.local/...?tableFilters[unit][reservable][code]=UN100000 Help?
9 Replies
dissto
disstoā€¢7mo ago
Well as I see it you have 2 simple options. Either pass the name "index" as first paremeter or use named arguments:
return $table
->...
->actions([
ActionGroup::make([
Action::make('...')
->...
->action(
fn($record) => redirect()->to(
ListOtherResourceFilament::getUrl('index', [
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
])
)
)
,
]),
]);
return $table
->...
->actions([
ActionGroup::make([
Action::make('...')
->...
->action(
fn($record) => redirect()->to(
ListOtherResourceFilament::getUrl('index', [
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
])
)
)
,
]),
]);
Or
return $table
->...
->actions([
ActionGroup::make([
Action::make('...')
->...
->action(
fn($record) => redirect()->to(
ListOtherResourceFilament::getUrl(parameters: [
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
])
)
)
,
]),
]);
return $table
->...
->actions([
ActionGroup::make([
Action::make('...')
->...
->action(
fn($record) => redirect()->to(
ListOtherResourceFilament::getUrl(parameters: [
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
])
)
)
,
]),
]);
šŸ¤”
TK
TKOPā€¢7mo ago
@dissto I'm sorry for being unclear. I know how to add the parameters, it's just not working. I end up with a URL like: https://example.local/...?tableFilters%5Bunit%5D%5Breservable%5D%5Bcode%5D=xxx That gets combined with the already active filters, and I end up with duplicate filters...
dissto
disstoā€¢7mo ago
Well the url is encoded, thats to be expected?! Can you use a url for testing?
Action::make()
->url(fn () => ListOtherResourceFilament::getUrl(parameters: [
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
])
))
Action::make()
->url(fn () => ListOtherResourceFilament::getUrl(parameters: [
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
])
))
TK
TKOPā€¢7mo ago
Not sure if it should encode.. I hoped not. Let me try your example @dissto Same thing.. 0 difference The tableFilters is now part of the url and not the filter part.. thats why Filament doesn't change it.. and keeps adding Don't get me wrong, it applies the filter, but whenever I add another or change the existing one to another value, the filter is being re-applied to the whole url, ending up with: https://example.local/....?tableFilters%5Bunit%5D%5Breservable%5D%5Bcode%5D=UN100000&tableFilters[unit][reservable][code]=UN100001 Laravel encodes the [ ]... since it's not a part of:
public $dontEncode = [
'%2F' => '/',
'%40' => '@',
'%3A' => ':',
'%3B' => ';',
'%2C' => ',',
'%3D' => '=',
'%2B' => '+',
'%21' => '!',
'%2A' => '*',
'%7C' => '|',
'%3F' => '?',
'%26' => '&',
'%23' => '#',
'%25' => '%',
];
public $dontEncode = [
'%2F' => '/',
'%40' => '@',
'%3A' => ':',
'%3B' => ';',
'%2C' => ',',
'%3D' => '=',
'%2B' => '+',
'%21' => '!',
'%2A' => '*',
'%7C' => '|',
'%3F' => '?',
'%26' => '&',
'%23' => '#',
'%25' => '%',
];
dissto
disstoā€¢7mo ago
I see what you mean now
->url(function ($record) {

$query = http_build_query([
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
]);

$query = urldecode($query);

$baseUrl = ListOtherResourceFilament::getUrl();

return $baseUrl.'?'.$query;
})
->url(function ($record) {

$query = http_build_query([
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->id
],
],
],
]);

$query = urldecode($query);

$baseUrl = ListOtherResourceFilament::getUrl();

return $baseUrl.'?'.$query;
})
Well maybe constructing the url yourself. Not sure if there is an easier way. Or even a way to not encode the route parameters, as this seems to be causing the issue. šŸ¤”
TK
TKOPā€¢7mo ago
Found it, it's a Filament bug. Square brackets [ ] should always be encoded, since not all browser recognizes them. The are not part of the allowed characters list. So Filament should always encode these, but they are not. Hence my problem: Laravel encodes them, Filament does not > duplicate values! Let me try your workaround.. @dissto 1 question though, whats the difference between using ->url() and ->action() with a redirect?
dissto
disstoā€¢7mo ago
Url is just a link...the action is an actual redirect. But you can use the same with an action..i was just using url in this case šŸ˜‹
TK
TKOPā€¢7mo ago
workaround works! thanks! I'll report the bug šŸ‘ Just for documentation sake, here's full the (workaround) solution:
return $table
->...
->actions([
ActionGroup::make([
Action::make('...')
->...
->url(function ($record) {
$query = http_build_query([
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->code,
],
],
],
]);

return ListOtherResourceFilament::getUrl() . '?' . urldecode($query);
})
,
]),
]);
return $table
->...
->actions([
ActionGroup::make([
Action::make('...')
->...
->url(function ($record) {
$query = http_build_query([
'tableFilters' => [
'unit' => [
'reservable' => [
'code' => $record->code,
],
],
],
]);

return ListOtherResourceFilament::getUrl() . '?' . urldecode($query);
})
,
]),
]);
TK
TKOPā€¢7mo ago
GitHub
Filament does not encode square brackets in the URL by default Ā· Is...
Package filament/filament Package Version v3.2.68 Laravel Version v11.10 Livewire Version v3.5.0 PHP Version PHP 8.2 Problem description Filament does not encode square brackets [ ] in the URL by d...
Want results from more Discord servers?
Add your server