Load different data based on the role

Hi guys, My project has 2 roles, "admin" and "contractor". There is a ClientCaseResource and If the logged in user is an admin, I want to list down all the client cases in the table. If the logged in uses is a contractor, I want to list down the client cases which are assigned only to that user. How to achieve this in Filament? TIA!
Solution:
```php $table ->modifyQueryUsing( fn (Builder $query) => $query->when( auth()->user()->hasRole('contractor'),...
Jump to solution
4 Replies
ChesterS
ChesterS5mo ago
You can change the query in the resource https://filamentphp.com/docs/3.x/panels/resources/getting-started#customizing-the-resource-eloquent-query something like
public static function getEloquentQuery(): Builder
{
return auth()->user()->is_admin
? ClientCaseResource::getQuery()
:ClientCaseResource::where('user_id', auth()->id()) ; // or auth()->user()->caseResources() if you have a relation
}
public static function getEloquentQuery(): Builder
{
return auth()->user()->is_admin
? ClientCaseResource::getQuery()
:ClientCaseResource::where('user_id', auth()->id()) ; // or auth()->user()->caseResources() if you have a relation
}
or something similar
Solution
LeandroFerreira
LeandroFerreira5mo ago
$table
->modifyQueryUsing(
fn (Builder $query) => $query->when(
auth()->user()->hasRole('contractor'),
fn (Builder $query) => $query->where('user_id', auth()->id())
)
)
$table
->modifyQueryUsing(
fn (Builder $query) => $query->when(
auth()->user()->hasRole('contractor'),
fn (Builder $query) => $query->where('user_id', auth()->id())
)
)
Pasindu
Pasindu5mo ago
Thank you very much. This is what I needed.
bogus
bogus5mo ago
@Pasindu check this documentation https://filamentphp.com/docs/3.x/panels/tenancy this is about tenancy but solution with space good enough better will be to do this mo globally.... especially if you have many records