sparmin
sparmin
FFilament
Created by sparmin on 2/5/2025 in #❓┊help
Custom Eloquent query shows wrong data in form() / User role management
I work a lot with user roles. The user role which is assigned to the logged in user has effect on the data and fields that are displayed. So far, it has worked like a charm. Now I have following requirement: Assuming we have the User "Dummy" who has the user role "admin". Inside the UserResource should be a list of all users who - do not have the user role "super_admin" or "admin" - unless it's the own user (so they can update their personal data). I tried resolving it with following custom Eloquent Query:
public static function getEloquentQuery(): Builder
{
$user = auth()->user();
if ($user->isSuperAdmin()) {
return parent::getEloquentQuery();
} else if ($user->isAdmin()) {
return parent::getEloquentQuery()->whereHas('roles', fn($query) => $query->whereNotIn('name', ['super_admin', 'admin']))->orWhere('id', $user->id);
}
}
public static function getEloquentQuery(): Builder
{
$user = auth()->user();
if ($user->isSuperAdmin()) {
return parent::getEloquentQuery();
} else if ($user->isAdmin()) {
return parent::getEloquentQuery()->whereHas('roles', fn($query) => $query->whereNotIn('name', ['super_admin', 'admin']))->orWhere('id', $user->id);
}
}
This results in showing the desired list of users inside table() with the own user being on top, so like this: Dummy User1 User2 ... But accessing any of the other users except Dummy always shows the wrong data. So User1, User2, etc show inside form() only the data of User1. The query itself does look fine, I checked the raw SQL and it gives me the desired data, just like seen with table(). The problem seems to be the last part: ->orWhere('id', $user->id) Removing this shows the correct data with form(), but of course without this the user cannot access their own data. I generally struggle understanding how the magic in the background exactly works, how does my custom query influence the data with table() or form() and why does my custom Eloquent Query not work with form()?
1 replies
FFilament
Created by sparmin on 1/22/2025 in #❓┊help
Filament-User vs. Frontend-User
Hi everyone! I'm newish to Filament and Laravel so I'm not sure what the best solution to this is: I want to build a tool consisting of a backend UI (Filament) with several panels and user roles. I also want a frontend with a super basic login functionality, as you see it on every site. What's the best way to handle these both user data? Should I have two different DB user tables? I installed Laravel Breeze and noticed that it interacts directly with the Filament users and sessions. What is the best approach here?
6 replies
FFilament
Created by sparmin on 11/4/2024 in #❓┊help
How to refresh form data?
No description
15 replies