0
I am stuck in an error here in Laravel and I don’t understand why it is occurring, the message itself I understood, that I am passing as parameter to the function a value that is string when it should be an instance of my model, but before it worked and did not understand why this error is occurring now, it follows the files:
Authserviceprovider.php
public function boot()
{
$this->registerPolicies();
$permissions = Permission::with('roles')->get();
foreach ($permissions as $permission) {
Gate::define($permission->name, function($user) use ($permission) {
return $user->hasPermission($permission->name);
});
}
}
A model: User.php
/**
* @param \Api\Users\Models\Permission $permission
*
* @return bool
*/
public function hasPermission(Permission $permission)
{
return $this->hasAnyRoles($permission->roles);
}
/**
* @param $roles
*
* @return bool
*/
public function hasAnyRoles($roles)
{
if(is_array($roles) || is_object($roles) ) {
return !! $roles->intersect($this->roles)->count();
}
return $this->roles->contains('name', $roles);
}
this is the error that occurs:
"Type error: Argument 1 passed to Api Users Models User::hasPermission() must be an instance of Api Acl Models Permission, string Given, called in C: wamp www restfulapi_test Infrastructure Providers Authserviceprovider.php on line 34"
Just try changing the $permission->name for the $permission object.
– arllondias
Should be
$this->hasAnyRoles($permission);
the correct, is not?– Guilherme Nascimento
I’m not sure, but I’ll take the test
– Leandro RR
@arllondias was just like that!
– Leandro RR