F
Filamentβ€’10mo ago
Diogo Pinto

modifyQueryUsing based on request - action resets query

Hey all, Use case: - In the /view it gets all categories that don't have parent ids - When you click on a record it takes you to route /{record}/view - In /{record}/view it fetches all subcategories where parent_id = {record} Problem: - In /{record}/view when I click on a Table action, the query resets and the action doesn't perform. Here's the code:
public static function table(Table $table): Table
{
return $table
//other code
->recordAction('view', Pages\ListProductCategories::route('/{record}'))
->actions([
Tables\Actions\ActionGroup::make([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
])
->modifyQueryUsing(fn (Builder $query): Builder =>
$query->when(request()->route('record'), function ($query) {
return $query->where('parent_id', request()->route('record'));
}, function ($query) {
return $query->where('parent_id', null);
}));
}

public static function getPages(): array
{
return [
'index' => Pages\ListProductCategories::route('/'),
'view' => Pages\ListProductCategories::route('/{record}/view'),
];
}
public static function table(Table $table): Table
{
return $table
//other code
->recordAction('view', Pages\ListProductCategories::route('/{record}'))
->actions([
Tables\Actions\ActionGroup::make([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
])
->modifyQueryUsing(fn (Builder $query): Builder =>
$query->when(request()->route('record'), function ($query) {
return $query->where('parent_id', request()->route('record'));
}, function ($query) {
return $query->where('parent_id', null);
}));
}

public static function getPages(): array
{
return [
'index' => Pages\ListProductCategories::route('/'),
'view' => Pages\ListProductCategories::route('/{record}/view'),
];
}
I'm wondering if I'm doing anything wrong or approaching the wrong way. Thanks!
Solution:
Add a $record property to your ListProductCategories page (not resource) because that es what you named to param.
Jump to solution
6 Replies
Dennis Koch
Dennis Kochβ€’10mo ago
You should never rely on request() in Livewire context. Use a property on the Livewire component instead.
Diogo Pinto
Diogo PintoOPβ€’10mo ago
Thanks @Dennis Koch
In the context of a Filament resource, what property would you suggest we use to check if a $record is set? πŸ‘ŒπŸ»
Solution
Dennis Koch
Dennis Kochβ€’10mo ago
Add a $record property to your ListProductCategories page (not resource) because that es what you named to param.
Dennis Koch
Dennis Kochβ€’10mo ago
Use $livewire->record inside the Closure then.
Diogo Pinto
Diogo PintoOPβ€’10mo ago
Gotchyaaa πŸ™πŸ» will provide feedback later. Thanks!
Diogo Pinto
Diogo PintoOPβ€’10mo ago
Worked like a charm @Dennis Koch πŸ’ͺ🏻

Did you find this page helpful?