How to integrate Laravel Policy in filament v4?

I integrate policy in the v4 like in v3 but glad to see not shown in the menu bar but if no policy it's shown.. is policy still used in v4? this is my sample policy
<?php

namespace App\Policies;

use App\Models\User;
use App\Enums\User\Role;
use Illuminate\Auth\Access\Response;

class UserPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return in_array($user->role, [
Role::Admin,
Role::SuperAdmin,
]);
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, User $model): bool
{
return in_array($user->role, [
Role::Admin,
Role::SuperAdmin,
]);
}

/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return in_array($user->role, [
Role::Admin,
Role::SuperAdmin,
]);
}

/**
* Determine whether the user can update the model.
*/
public function update(User $user, User $model): bool
{
return in_array($user->role, [
Role::Admin,
Role::SuperAdmin,
]);
}

/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, User $model): bool
{
return false;
}

/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, User $model): bool
{
return false;
}

/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, User $model): bool
{
return false;
}
}
<?php

namespace App\Policies;

use App\Models\User;
use App\Enums\User\Role;
use Illuminate\Auth\Access\Response;

class UserPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return in_array($user->role, [
Role::Admin,
Role::SuperAdmin,
]);
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, User $model): bool
{
return in_array($user->role, [
Role::Admin,
Role::SuperAdmin,
]);
}

/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return in_array($user->role, [
Role::Admin,
Role::SuperAdmin,
]);
}

/**
* Determine whether the user can update the model.
*/
public function update(User $user, User $model): bool
{
return in_array($user->role, [
Role::Admin,
Role::SuperAdmin,
]);
}

/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, User $model): bool
{
return false;
}

/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, User $model): bool
{
return false;
}

/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, User $model): bool
{
return false;
}
}
Solution:
Ahh the problem is I forgot to cast the role.. thanks anyways
Jump to solution
2 Replies
toeknee
toeknee2mo ago
Yes policies are still used, did you check if the gates/policies are triggered in the view with debugbar or telecope? You may have not registered it?
Solution
Jerome V
Jerome V2mo ago
Ahh the problem is I forgot to cast the role.. thanks anyways

Did you find this page helpful?