Argument 1 passed to App\Models\User::hasAnyRoles() must be of the type array, string given

2.1k views Asked by At

i want to limit admins from accessing certain pages and am getting this error after running my code: Argument 1 passed to App\Models\User::hasAnyRoles() must be of the type array, string given, called in F:\Main Server\htdocs\voskillproject\app\Providers\AuthServiceProvider.php on line 33 (View: F:\Main Server\htdocs\voskillproject\resources\views\backend\adminsidebar.blade.php)

here is my user model

public function roles(){
    return $this->belongsToMany('App\Models\Role');

}

/**
 * check if the user has a role
 * @param string $role
 * @return bool
 */
public function hasAnyRole(string $role)
{
    return null !== $this->roles()->where('Role_name',$role)->first();
}

/**
 * check if the user has any given role
 * @param array $role
 * @return bool
 */
public function hasAnyRoles(array $role)
{
    return null !== $this->roles()->whereIn('Role_name',$role)->first();
}

here is my authserviceprovider where i registered my gate

  Gate::define('is-admin',function($user)
  {
    return $user->hasAnyRoles('Super Admin','Company Manager');
  }); 

here is the navbar section

  @can('is-admin')
    <li class="nav-item {{ 'admin/roles'==request()->path()?'active':' ' }}">
      <a class="nav-link" href="{{ route('roles.index') }}">
        <i class="mdi mdi-view-headline menu-icon"></i>
        <span class="menu-title">Roles</span>
      </a>
    </li>
  @endcan

i've not understood where i did wrong on my code

1

There are 1 answers

0
stephen waweru99 On

i found the solution in the authserviceprovider instead of hasAnyRoles it should be hasAnyRole