F
Filamentβ€’3mo 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β€’3mo ago
You should never rely on request() in Livewire context. Use a property on the Livewire component instead.
Diogo Pinto
Diogo Pintoβ€’3mo 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β€’3mo ago
Add a $record property to your ListProductCategories page (not resource) because that es what you named to param.
Dennis Koch
Dennis Kochβ€’3mo ago
Use $livewire->record inside the Closure then.
Diogo Pinto
Diogo Pintoβ€’3mo ago
Gotchyaaa πŸ™πŸ» will provide feedback later. Thanks!
Diogo Pinto
Diogo Pintoβ€’3mo ago
Worked like a charm @Dennis Koch πŸ’ͺ🏻
Want results from more Discord servers?
Add your server
More Posts
Printable layout for index pagesHi there, I'm looking to a proper way to render a print friendly view of resource index pages (ListRImport action download example button missing (BUG)After i added modalDescription with ``` new HtmlString() ``` download example csv is now missing in disableOptionWhenIn the Relation Manager form I am using a Select. However, I want to disable the option that has alrGet $get function doesn't retrieve value from a field in a different FieldsetHi, I'm trying to access the value of 'club_id' using $get('club_id'), however it always returns nuThe data is not displaying in the Filament Table Builder v2.Hello! Please help me, I'm using PHP 8.0, Laravel 8, and Livewire 2. I installed Filament Table BuilIs it possible to register livewire endpoints for columns?I'm trying to come up with how I can create more advanced custom column types in filament, but as faIs it possible to make table on custom page without eloquent?I want to make POS with livewire, and I can't figure out how I can make table with reactive array daHelp with database table names and model, resource folder structureHello everybody. Sorry, this is completely off topic, but I need help now. I think there are peopleMail problem rendering view with data with two arraysHello. I have a row action which lets users send email with attached Pdf with some info about the reGetting event after record creation (afterCreate not working here)I have 2 tables `contacts` and `contacts_profile`. I have a form to create a contact (and also fill