Jump24 - Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Blade Authorization Directives for View Security

Last updated on by

Blade Authorization Directives for View Security image

Laravel's Blade authorization directives integrate seamlessly with your application's permission system, enabling clean conditional rendering based on user capabilities. These directives maintain consistency between backend authorization and frontend display logic.

Blade provides three primary authorization directives for permission checking:

@can('edit', $article)
<button class="edit-btn">Edit Article</button>
@endcan
 
@cannot('delete', $article)
<span class="text-muted">Deletion not permitted</span>
@endcannot

The @canany directive checks multiple permissions simultaneously, useful for complex authorization scenarios.

@canany(['publish', 'schedule'], $article)
<div class="publishing-controls">
Publishing options available
</div>
@endcanany

Here's a comprehensive dashboard implementation showcasing various authorization patterns:

<div class="dashboard-layout">
<header class="dashboard-header">
<h1>Content Management</h1>
 
@canany(['create-posts', 'create-pages'])
<div class="create-actions">
@can('create-posts')
<a href="{{ route('posts.create') }}" class="btn btn-primary">New Post</a>
@endcan
 
@can('create-pages')
<a href="{{ route('pages.create') }}" class="btn btn-secondary">New Page</a>
@endcan
</div>
@endcanany
</header>
 
<main class="content-area">
@foreach($articles as $article)
<article class="content-card">
<h2>{{ $article->title }}</h2>
<p>{{ $article->excerpt }}</p>
 
<div class="article-meta">
<span>By {{ $article->author->name }}</span>
<span>{{ $article->published_at->diffForHumans() }}</span>
</div>
 
<div class="article-actions">
@can('update', $article)
<a href="{{ route('articles.edit', $article) }}" class="action-link">
Edit
</a>
@endcan
 
@can('publish', $article)
@if($article->is_draft)
<form method="POST" action="{{ route('articles.publish', $article) }}" class="inline-form">
@csrf
<button type="submit" class="publish-btn">Publish</button>
</form>
@endif
@endcan
 
@cannot('delete', $article)
<span class="disabled-action">Delete (Protected)</span>
@else
<form method="POST" action="{{ route('articles.destroy', $article) }}" class="inline-form">
@csrf
@method('DELETE')
<button type="submit" class="delete-btn" onclick="return confirm('Delete this article?')">
Delete
</button>
</form>
@endcannot
</div>
</article>
@endforeach
</main>
 
<aside class="sidebar">
@canany(['view-analytics', 'manage-users', 'system-settings'])
<div class="admin-panel">
<h3>Administration</h3>
<ul class="admin-links">
@can('view-analytics')
<li><a href="{{ route('analytics.dashboard') }}">Analytics</a></li>
@endcan
 
@can('manage-users')
<li><a href="{{ route('users.index') }}">User Management</a></li>
@endcan
 
@can('system-settings')
<li><a href="{{ route('settings.index') }}">System Settings</a></li>
@endcan
</ul>
</div>
@endcanany
 
@guest
<div class="login-prompt">
<p>Please <a href="{{ route('login') }}">login</a> to access all features.</p>
</div>
@else
@cannot('create-posts')
<div class="upgrade-notice">
<p>Upgrade your account to create posts.</p>
<a href="{{ route('plans.index') }}" class="upgrade-link">View Plans</a>
</div>
@endcannot
@endguest
</aside>
</div>

Authorization directives ensure that UI elements align with user permissions, creating secure and intuitive interfaces while reducing the risk of exposing unauthorized functionality.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Bacancy

Outsource a dedicated Laravel developer for $3,200/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Detecting and Fixing Race Conditions in Laravel Applications image

Detecting and Fixing Race Conditions in Laravel Applications

Read article
LaraCopilot: Generate Laravel MVPs From a Single Prompt With AI image

LaraCopilot: Generate Laravel MVPs From a Single Prompt With AI

Read article
Model::withoutRelation() in Laravel 12.54.0 image

Model::withoutRelation() in Laravel 12.54.0

Read article
Tyro Checkpoint: Instant SQLite Snapshots for Laravel Local Development image

Tyro Checkpoint: Instant SQLite Snapshots for Laravel Local Development

Read article
The Laravel Community Mobile App Helps You Discover Events and Connect With Developers image

The Laravel Community Mobile App Helps You Discover Events and Connect With Developers

Read article
A PHP Package for Concurrent Website Crawling image

A PHP Package for Concurrent Website Crawling

Read article