F
Filamentβ€’7mo ago
DOUBLEPROFIT

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β€’7mo ago
What are you trying to do? Don't detail the technical implementation, what is the use-case you're looking to support?
DOUBLEPROFIT
DOUBLEPROFITβ€’7mo 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
DOUBLEPROFIT
DOUBLEPROFITβ€’7mo ago
This should be the expected behaviour when clicking on reset filter button. It should query records according to getEloquentQuery()
Expected reset filter behaviour
DOUBLEPROFIT
DOUBLEPROFITβ€’7mo 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β€’7mo ago
Right, you want to modifyBaseQuery, I believe.
Solution
Oddman
Oddmanβ€’7mo 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β€’7mo ago
That's not 100% right code, you'll need to fix it - but that's kinda what you want.
DOUBLEPROFIT
DOUBLEPROFITβ€’7mo ago
That works, thanks bro!
Oddman
Oddmanβ€’7mo ago
Remember to mark the right answer πŸ™‚
Want results from more Discord servers?
Add your server
More Posts