agyanadda@gmail.com

Laravel Authorization: Gates and Policies

Forms, Security and APIs 1 min read Updated Dec 2, 2026 Topic 23 of 31

Authentication asks who are you? Authorization asks are you allowed to do this? — for example, only the author may edit a post.

Gates

A Gate is a simple yes/no rule, usually defined in AppServiceProvider::boot():

Gate::define('view-admin', fn (User $user) => $user->is_admin);

Policies

A Policy groups rules for one model. php artisan make:policy PostPolicy --model=Post creates methods such as view, update and delete. Laravel finds PostPolicy for Post automatically.

Checking permission

  • Gate::authorize('update', $post); — throws a 403 if not allowed
  • $request->user()->can('update', $post) — returns true or false
  • ->can('update', 'post') on a route — as middleware
  • In Blade: @can('update', $post) ... @endcan to show or hide buttons

Remember

Hiding a button is not security. Always check in the controller or route too, because anyone can send a request directly.

Example

Example — Only the author can edit PHP
<?php
// app/Policies/PostPolicy.php
public function update(User $user, Post $post): bool
{
    return $user->id === $post->user_id || $user->is_admin;
}

public function delete(User $user, Post $post): bool
{
    return $this->update($user, $post);
}

// app/Http/Controllers/PostController.php
use Illuminate\Support\Facades\Gate;

public function edit(Post $post)
{
    Gate::authorize('update', $post);   // 403 for other users

    return view('posts.edit', compact('post'));
}

/* Blade:
@can('update', $post)
    <a href="{{ route('posts.edit', $post) }}">Edit</a>
@endcan */

The author and admins see the Edit link and can open the page. Another user does not see the link, and opening the URL directly shows a 403 Forbidden page.

Test Yourself With Exercises

Exercise 1 Multiple Choice

What HTTP status does a failed Gate::authorize() return?

Exercise 2 True / False

Hiding the Edit button with @can is enough to protect the edit page.

Exercise 3 Fill in the Blank

Create a policy with php artisan make:___ PostPolicy --model=Post

Frequently Asked Questions

Use Policies for rules about a model (edit post, delete comment) and Gates for general actions not tied to one model (view admin dashboard, see reports). For many roles and permissions, a package such as spatie/laravel-permission helps.