Eager Loader API Resource to get Roles and Permissions #1691
Unanswered
leo95batista
asked this question in
Q&A
Replies: 1 comment 1 reply
-
If you don't want the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone, I am just starting to use this project and I think it is great. Thank you very much for the work done. It happens that in my particular case I am trying to return an api resource with the details of the user and next to them the roles and permissions that the user has.
My
UserResorce.php
My
RoleResource.php
My
PermissionResource.php
As you can see in the
UserResource.php
file, I only load theroles
andpermissions
when they are really needed. In this way if I do the following:I get the following result:
So to load the same result but this time with the roles associated with the user I do the following:
and as expected I get the following result:
Now my problem is the following, to get the user roles and permissions I did the following:
but I get an empty object in the
"permissions"
key, so I analyzed more carefully and it is that the permissions that user has are defined by theirrole
, I looked in the documentation and I saw that I can obtain all the permissions of a user usinggetAllPermissions()
method. So I did the following:but in this way, it also loads the
permissions
in the roles relation and it would obtain an unexpected result like the following:and it is not the expected result, since the result that I expect when executing:
should by the following:
and when execute:
I expect the following result:
Could someone help me with this problem? Thank you very much in advance.
Beta Was this translation helpful? Give feedback.
All reactions