1

In my RoleAndPermission seeder I have

        $admin = Role::create(['name' => 'admin']);
     
        Permission::create(['name' => 'create-attribute']);
        Permission::create(['name' => 'delete-attribute']);
        Permission::create(['name' => 'edit-attribute']);
        Permission::create(['name' => 'update-attribute']);
        Permission::create(['name' => 'view-attribute']);
        Permission::create(['name' => 'store-attribute']);
        $admin->givePermissionTo([
            'create-attribute',
            'view-attribute',
            'delete-attribute',
            'edit-attribute',
            'update-attribute',
            'store-attribute'
        ]);

I have created AttributePolicy

public function viewAny(User $user)
      {
        return $user->hasPermissionTo('view-attribute');    
      }

And in Auth service provider gate

 public function boot()
    {
        $this->registerPolicies();

        Gate::define('view-attribute', [AttributePolicy::class, 'viewAny']);
    }

and in my AttributeController

public function index(Request $request)
    {
        if (Gate::allows('view-attribute')) {
        return Inertia::render('Admin/Attribute/Index', []);
        }
    }

but when i try to access attribute index page as an admin i get error Call to a member function contains() on array. I know it is problem with return $user->hasPermissionTo('view-attribute'); but i don't know why, can somebody help me please?

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Emia
  • 55
  • 8
  • In which file and on what line is this error reported (I assume somewhere inside the framework files)? Have you tried to dump the values throughout the functions in the stack trace to identify where a wrong parameter might have been passed? – El_Vanja Nov 26 '20 at 11:11
  • It is in attribute policy line return $user->hasPermissionTo('view-attribute'); because when i write return $user->hasRole('admin'); and not use gate just middlware can on my rout it works. But in my database it said that admin have this permissions and this user have role admin. – Emia Nov 26 '20 at 11:17
  • it says Error Call to a member function contains() on array App\Models\User::hasDirectPermission D:\xxx\xxx\xxx\xxx\vendor\spatie\laravel-permission\src\Traits\HasPermissions.php:285 @El_Vanja – Emia Nov 26 '20 at 11:35

0 Answers0