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) ... @endcanto 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
<?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.