F
Filamentβ€’12mo ago
Jia Yong Lim

Reset Filter according to getEloquentQuery

Hi, I'm new to filament, I'm facing an issue on resetting filters, appreciate any help πŸ˜… Expected behaviour: Reset filter on index page and get it to query according to getEloquentQuery() Current behaviour: Clicking on reset button will reset everything and ignore queries in getEloquentQuery(). Code:
public static function getEloquentQuery(): Builder
{
$operation = last(explode('.', Route::currentRouteName()));
$query = parent::getEloquentQuery();

if ($operation == 'index')
{
$authUser = auth()->user();

if ($authUser->role == 'restaurant-admin') {
$rewardIds = Reward::where('restaurant_id', $authUser->restaurant_id)->pluck('id');
$query = $query->whereIn('reward_id', $rewardIds);
}
}

return $query
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
public static function getEloquentQuery(): Builder
{
$operation = last(explode('.', Route::currentRouteName()));
$query = parent::getEloquentQuery();

if ($operation == 'index')
{
$authUser = auth()->user();

if ($authUser->role == 'restaurant-admin') {
$rewardIds = Reward::where('restaurant_id', $authUser->restaurant_id)->pluck('id');
$query = $query->whereIn('reward_id', $rewardIds);
}
}

return $query
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
Solution:
Eg. On your ListResource page: ```php public function table(Table $table): Table {...
Jump to solution
9 Replies
Oddman
Oddmanβ€’12mo ago
What are you trying to do? Don't detail the technical implementation, what is the use-case you're looking to support?
Jia Yong Lim
Jia Yong LimOPβ€’12mo ago
In the index page, I am trying to filter records according to authenticated user role. Admins can see all records. Restaurant admins can only see records belong to them. Initially, I can get it to filter records correctly according to getEloquentQuery(). But the filter doesn't work anymore once I click 'reset' button on the filter modal. In other words, when restaurant admin clicks on the reset filter button, all records are shown even if it doesn't belong to them (as shown in the image attached.
unexpected behaviour
Jia Yong Lim
Jia Yong LimOPβ€’12mo ago
This should be the expected behaviour when clicking on reset filter button. It should query records according to getEloquentQuery()
Expected reset filter behaviour
Jia Yong Lim
Jia Yong LimOPβ€’12mo ago
Anyways, I just found the solution, getEloquentQuery() should have this line:
if (in_array($operation, ['index', 'update']))
if (in_array($operation, ['index', 'update']))
instead of:
if ($operation == 'index')
if ($operation == 'index')
appreciate you trying to help @Oddman 🍻
Oddman
Oddmanβ€’12mo ago
Right, you want to modifyBaseQuery, I believe.
Solution
Oddman
Oddmanβ€’12mo ago
Eg. On your ListResource page:
public function table(Table $table): Table
{
$table = parent::table($table);

if ($authUser->role == 'restaurant-admin') {
$rewardIds = Reward::where('restaurant_id', $authUser->restaurant_id)->pluck('id');

$table->modifyQueryUsing(fn(QueryBuilder $query) => $query->whereIn('reward_id', $rewardIds));
}

return $table;
}
public function table(Table $table): Table
{
$table = parent::table($table);

if ($authUser->role == 'restaurant-admin') {
$rewardIds = Reward::where('restaurant_id', $authUser->restaurant_id)->pluck('id');

$table->modifyQueryUsing(fn(QueryBuilder $query) => $query->whereIn('reward_id', $rewardIds));
}

return $table;
}
Oddman
Oddmanβ€’12mo ago
That's not 100% right code, you'll need to fix it - but that's kinda what you want.
Jia Yong Lim
Jia Yong LimOPβ€’12mo ago
That works, thanks bro!
Oddman
Oddmanβ€’12mo ago
Remember to mark the right answer πŸ™‚
Want results from more Discord servers?
Add your server