On ListResource.php - How to show only the resources which belong to the current user?

I am using filament's plugin for Spatie Laravel Permissions, and everything works just fine except this thing. I dont want to hide the whole resource from the navigation bar so viewAny() along with $post->user_id === auth()->user() won't fit here..
10 Replies
krekas
krekas8mo ago
You should have field like user_id in the db and modify the query
prowler
prowlerOP8mo ago
thanks @krekas!
Povilas K
Povilas K8mo ago
@prowler spatie permission package is more for resource permission. What you're talking about is record permission which is called multi-tenancy, so yes as @krekas said filter it by user_id on resource query level or in Eloquent with global scopes.
!$mileFox!
!$mileFox!8mo ago
I think so
prowler
prowlerOP8mo ago
Not sure im talking about multi-tenancy in Filament's terms. I simply have 3 different Roles for users (Client, Editor and Super Admin) using spatie's package which lets me fine-grain their permissions per resource (a requirement i have). Each role may or may not be visible to different resources (or in some cases, to resource's specific fields). Each user associated with a role and for example, when a Client logs in - I simply want them to see their data only. Each user belongs to 1 tenant only, while
Filament's tenancy system implies that the user belongs to many tenants (organizations, teams, companies, etc.) and may switch between them.
krekas
krekas8mo ago
if you want user to see only his own records you must add some field where you would attach usually users id. this field usually is user_id
prowler
prowlerOP8mo ago
oh yeah, ofc. i already have this field. what i was missing is the query modification thingy, which u answered
->modifyQueryUsing(function (Builder $query) {
if (Auth::user()->hasRole(['Editor', 'Super Admin'])) {
return $query;
} else {
return $query->where('user_id', Auth::id());
}

})
->modifyQueryUsing(function (Builder $query) {
if (Auth::user()->hasRole(['Editor', 'Super Admin'])) {
return $query;
} else {
return $query->where('user_id', Auth::id());
}

})
krekas
krekas8mo ago
yes. this is what you need you could call it a tenancy in a way
Povilas K
Povilas K8mo ago
Yeah the term tenancy has so many meanings and cases. In this case, user is a tenant.
prowler
prowlerOP8mo ago
Thank u all! @PovilasKorop - btw, a big fan of your stuff! keep up the good work
Want results from more Discord servers?
Add your server