Policies and Gates for Authorization
Beyond broad roles, applications often need to answer more specific questions: can this user edit this particular post? Laravel handles this kind of fine-grained authorization through two related tools — Gates and Policies.
Gates
A Gate is a simple closure-based way to define authorization logic, typically registered in a service provider. Gates work well for actions that aren't tied to a specific Eloquent model, like accessing an admin dashboard.
Policies
A Policy is a dedicated class for organizing authorization logic around a specific model, such as a Post or Comment. Each method on a policy typically corresponds to an action, like update or delete, and receives the current user along with the relevant model instance.
| Tool | Best For |
|---|---|
| Gate | Simple, model-independent checks, like admin access |
| Policy | Authorization logic scoped to a specific Eloquent model |
Gate::allows() | Checking a gate's result directly in code |
$user->can('update', $post) | Checking a policy method through the authorizable user |
@can (Blade) | Conditionally showing UI based on an authorization check |
Why Separate Authorization From Business Logic
- Keeps controllers focused on actions, not permission checks
- Makes authorization rules reusable across controllers, views, and API responses
- Provides a single, testable place to verify who can do what
Policies and gates decide what a request is allowed to do once it reaches a controller — but often you want to intercept a request even earlier, before it gets that far. That's exactly the role middleware plays in a Laravel application.
Defining a Gate and a Policy in Code
A gate is usually defined in the boot method of a service provider such as AppServiceProvider:
use Illuminate\Support\Facades\Gate;
use App\Models\User;
public function boot(): void
{
Gate::define('view-admin-dashboard', function (User $user) {
return $user->is_admin;
});
}
A policy is generated with Artisan and linked to its model:
php artisan make:policy PostPolicy --model=Post
class PostPolicy
{
public function update(User $user, Post $post): bool
{
return $user->id === $post->user_id;
}
public function delete(User $user, Post $post): bool
{
return $user->id === $post->user_id || $user->is_admin;
}
}
When the policy follows the naming convention (a Post model and a PostPolicy in the policies folder), Laravel discovers it automatically. Otherwise you can register it manually with Gate::policy(Post::class, PostPolicy::class).
Using the Checks in Controllers, Routes and Views
Inside a controller, Gate::authorize() throws an exception that becomes a 403 response if the check fails, so the rest of the method never runs:
public function update(Request $request, Post $post)
{
Gate::authorize('update', $post);
$post->update($request->validated());
return redirect()->route('posts.show', $post);
}
On a route, the can middleware performs the same check before the controller is reached:
Route::put('/posts/{post}', [PostController::class, 'update'])
->middleware('can:update,post');
In Blade, show or hide buttons with the same rule:
@can('delete', $post)
<button type="submit">Delete</button>
@endcan
Remember that hiding a button is only for user experience. The server-side check is what actually protects the action.
Outside controllers, you can ask the user model directly with $request->user()->can('update', $post) or its opposite, cannot(). For actions that do not need a model instance, such as creating a new post, pass the class name instead: Gate::authorize('create', Post::class). Laravel then calls the policy's create method with only the user.
Handy Extras: before() and Responses
A policy can include a before method that runs ahead of every other check. Returning true grants access, false denies it, and null lets the normal method decide. It is a tidy place to give a super admin full access. Policy methods can also return Response::deny('You do not own this post.') to send a clearer message than a plain false. For actions where no user is logged in, type-hint the user as nullable (?User $user) so the check runs instead of failing automatically.
Common Authorization Mistakes
- Checking permission in the view but not in the controller, so a crafted request still succeeds.
- Putting ownership checks directly inside controllers, duplicating logic that a policy should own.
- Forgetting to authorize list endpoints, which can leak records the user should not see; filter queries as well.
- Making
before()returnfalsefor everyone else by accident, which blocks every policy method. - Not writing tests. A feature test that asserts
assertForbidden()for a non-owner catches regressions quickly.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.