简体   繁体   中英

Method Illuminate\Database\Eloquent\Collection::addEagerConstraints does not exist LARAVEL

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;
    }
}

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM