-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need to conditionally load user permissions (abilities) via eloquent as flattened array #2793
Comments
Not an issue, must be a discussion |
@parallels999 Okay, but still my discussion is still a valid point, how might we achieve this functionality with the package to reduce and optimise the number of queries. Maybe this could be a new feature? |
@sts-ryan-holton You mentioned that part of the concern you raised is related to lazy-loading. |
Just eager load relations User::with(['roles', 'roles.permissions', 'permissions'])->all(); Now |
This appears to work without errors: public function getAbilitiesAttribute(): array
{
try {
return $this
->loadMissing(['roles', 'roles.permissions', 'permissions'])
->getAllPermissions()->pluck('name')->flatten()->toArray();
} catch (\Exception $err) {
}
return [];
} |
I don't want to load it everywhere via my |
Do you mean that you'd just prefer that this package build it into the trait, so that it becomes part of the User model?
Can you give an example of how you'd use this in a controller? Third, what would be the "ideal" interface that you have in mind? |
Description
Hi, I need to be able to load "abilities" for a user within my system as a flattened array. Note that a
User
isn't always the logged in user and is sometimes a tenant of the system. Right now I'm doing this with an appended attribute, but this is loaded everywhere and is calling the Lazy load error.How can I reproduce the following function but via eloquent when doing
User::all()
in the system:Steps To Reproduce
n/a
Example Application
n/a
Version of spatie/laravel-permission package:
6.10.1
Version of laravel/framework package:
11.38.2
PHP version:
8.3.8
Database engine and version:
MySQL 8
OS: Windows/Mac/Linux version:
ac
The text was updated successfully, but these errors were encountered: