0
votes

I need help, don't see anything suspicious :c thanks for help !

Error Collection::addEagerConstraints does not exist occurs after the call:

 public function show(Request $request, User $user)
 {
        $user->load('permissions');
        dd($with);
        return UserResource::make($user);
 } 

User Model:

class User extends Authenticatable
{
    (...)

    //////////////////////////////////////////////////////////////////////////////////////////
    ///
    ///  Relationships
    ///
    //////////////////////////////////////////////////////////////////////////////////////////

    /**
     * Relationship to permissions
     *
     * @return RolePermissions
     */
    public function permissions()
    {
        return $this->role()->first()->permissions;
    }
}
1
try : return $this->role()->with('permissions')->get()-> first()->permissions; - OMR
Do you have a table permissions? Or is permissions just a column in roles? - Tim Lewis
@OMR That still won't work; you can't call ->load() on a non-relationship, and unless Permission is a model (which I suspect it isn't), this code needs to be changed. - Tim Lewis
if you are using standard laravel user , u could simply remove your 'permissions' relation and use the ready-made one: $permissionNames = $user->getPermissionNames(); // collection of name strings $permissions = $user->permissions; // get the user permissions - OMR
Thanks I resolved problem :) Thank you very much - Bartłomiej Łaski

1 Answers

0
votes

if you are using standard laravel user , you have to remove your 'permissions' relation and use the ready-made one:

$permissionNames = $user->getPermissionNames(); // collection of name strings 
$permissions = $user->permissions; // get the user permissions 

if you want a user with its permissions:

$user->load('permissions');

more details in:

https://docs.spatie.be/laravel-permission/v3/basic-usage/basic-usage/