Controller, gate
- Code
- Gate::authorize('update-post', $post)
- Result
- Throws AuthorizationException, converted to a 403
Trace a Laravel 403 to its source: a failed Gate or policy, the can middleware, abort(403) or a guest user. Includes policy fixes and custom error page steps.
In Laravel, a 403 usually means an authorization check said no. Gate::authorize, a policy method, the can middleware or an explicit abort(403) throws an exception that the framework turns into a 403 response. Find the check on that route, then fix the policy logic, the user, or the model it receives.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
Search your routes and controllers for those calls, starting with the route that fails. The can middleware often hides in a route group, so check group definitions as well as the single route.
Then list the middleware on the failing route and ask the framework what the policy says. These commands run inside your Laravel project:
php artisan route:list --path=posts
# Expect a table with a Middleware column. Look for can:update,post or an auth group.
php artisan tinker
# >>> Gate::forUser(App\Models\User::find(1))->allows('update', App\Models\Post::find(1))
# Expect true or false for that user and record.If tinker says false, the policy logic or the data is the cause, not routing. If it says true and the route still returns 403, a different check is firing, such as an explicit abort(403) or middleware.
curl -i https://httpbin.org/status/403
# Expect: HTTP/2 403. Use the same -i flag on your own route to see the status line
# and whether the body is HTML or JSON.user_id:public function update(User $user, Post $post): bool
{
return $user->id === $post->user_id;
}public function update(?User $user, Post $post): bool
{
return $user?->id === $post->user_id;
}use Illuminate\Auth\Access\Response;
public function update(User $user, Post $post): Response
{
return $user->id === $post->user_id
? Response::allow()
: Response::denyAsNotFound();
}Strict comparison with === fails when one side is a string and the other an integer, which can happen with values from request input or an unusual driver. Check the types in tinker before assuming a permissions problem. Also confirm the policy is found: by default Laravel discovers a policy when the model and policy follow its naming conventions, so a Post model needs a PostPolicy in a Policies directory at or above the models. With no registered policy, can falls back to a closure Gate of the same name.
Guests are a special case. By default every gate and policy returns false when the request was not made by an authenticated user. A signed-out visitor hitting an authorize call therefore gets a 403 rather than a login prompt. To let guests through to the check, make the user argument optional:
If you would rather not reveal that a resource exists, the docs offer Response::denyAsNotFound(), or Response::denyWithStatus(404) for another code, in place of the default 403:
resources/views/errors/403.blade.php. The error handling docs say views in that directory are named for the status code they serve, and the HttpException raised by abort is passed in as $exception.Publish the default templates with php artisan vendor:publish --tag=laravel-errors if you want a starting point. A 4xx.blade.php file acts as a fallback for codes without their own page. For JSON clients, Laravel picks HTML or JSON by the request's Accept header, and you can change that rule with shouldRenderJsonWhen.
<h2>{{ $exception->getMessage() }}</h2>Google treats 4xx codes other than 429 alike: content from those URLs is ignored, and 401 and 403 have no effect on crawl rate. Policies usually guard account pages that should not be indexed anyway. Trouble starts when a policy or can middleware wraps a public route, so Googlebot, which is not signed in, gets a 403. Fetch the URL with curl as a guest to see what a crawler sees. For finding affected URLs, see 4xx errors.
3 questions people ask next, answered from the sources on this page.
It throws an AuthorizationException when the action is not allowed, and Laravel's exception handler converts that into a 403 response.
Gates and policies return false for guests by default, so an authorize call fails with 403. Protect the route with the auth middleware first if you want unauthenticated users sent to login.
Return Response::denyAsNotFound() from the policy or gate, or Response::denyWithStatus() with the status you want.
Run the free audit, then approve the fixes. Every change can be undone.