The policies are automatically declared and attached to the right model if you apply strictly the naming convention.
Model / ModelPolicy BoardItem / BoardItemPolicy and not BoardItemFilePolicy
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
why does my controller actions always returns a 403 even if the policy returns true?
I'm using Laravel 11, I have BoardItemFile model and BoardItemFilePolicy. Even if the conditions are met, it still returns 403.
What I did:
true on the policy method - still failsmy policy:
class BoardItemFilePolicy
{
public function before(User $user, string $ability) : bool | null {
if ($user->userRole->role == 0) {
return true;
}
return null;
}
public function create(User $user, BoardItem $item): bool
{
return true;
// return $user->id == $item->user_id;
}
and the controller method:
public function store(Workspace $workspace, Board $board, BoardItem $item, Request $request)
{
if ($request->user()->cannot('create', $item)) {
abort(403);
}
I also tried Gate::authorize('create', [$item]) and returns the same thing.
I'm not sure why this particular policy fails as I have more policy which works fine and expected.
Please or to participate in this conversation.