How to Override the Authorizable Can Method in Laravel Policy
In today's lesson, we are going to see how to override the authorizable can method in Laravel policy, let's assume that we have a policy that authorizes user actions such as viewing, creating, updating, and deleting his own posts and we want the admin to have the same privileges but for all the posts.
Use the before method
To authorize all actions we use the before method, which will be executed before any other method, and checks if the user is an admin if yes he is authorized to perform all actions.
use App\Models\User;
/**
* Perform pre-authorization checks.
*/
public function before(User $user, string $ability): bool|null
{
if ($user->isAdmin()) {
return true;
}
return null;
}