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:
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()?0 Replies