You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
{
return parent::getEloquentQuery()->whereHas('roles', fn($query) => $query->whereNotIn('name', ['super_admin', 'admin']))->orWhere('id', auth()->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()?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Package
Form builder
Package Version
v3.2.92
How can we help you?
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:
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()?
Beta Was this translation helpful? Give feedback.
All reactions