Moum
Moum

Reputation: 93

Restrict access in Laravel Nova

i'm using nova in a project and i want to give acces to dashboard to admin user only. So i try this according to the doc, but i don't know why it's not working. Anyone can hep me to achieve this ! thank's

method gate of NovaServiceProvider here:

 protected function gate()
{
    Gate::define('viewNova', function ($user) {
        $this->isAdmin($user);
    });
}

**here i checked if admin user** 

public function isAdmin(User $user)
{
    return $user->type == 3 ? true : false;
}

Upvotes: 1

Views: 1400

Answers (2)

Noorbala7418
Noorbala7418

Reputation: 52

I suppose you that add a column on your database table for permission. For example, your permissions is: admin & user

so insert below on your migration: $table->enum('permission', ['admin', 'user'])->default('user');

(sorry because of my awful english) see it: https://laravel.com/docs/8.x/migrations#column-method-enum

Upvotes: 0

Saumini Navaratnam
Saumini Navaratnam

Reputation: 8850

I believe it is because you forgot to return it.

protected function gate()
{
    Gate::define('viewNova', function ($user) {
        return $this->isAdmin($user); // You need to return
    });
}

Upvotes: 3

Related Questions