Laravel Tutorials

Enhancing Laravel Authorization with Backed Enums

Published
Enhancing Laravel Authorization with Backed Enums image

Laravel now offers direct support for backed enums in authorization, eliminating the need to reference the enum's value property. This streamlined approach makes permission checks more intuitive and maintainable.

The implementation is straightforward:

enum ReportPermission: string
{
case VIEW = 'reports.view';
case EXPORT = 'reports.export';
case MANAGE = 'reports.manage';
}
 
public function showReport(): View
{
$this->authorize(ReportPermission::VIEW);
 
return view('reports.show');
}

This pattern works elegantly across complex authorization systems:

enum ContentPermission: string
{
case LIST_ARTICLES = 'content.articles.list';
case VIEW_ARTICLE = 'content.articles.view';
case CREATE_ARTICLE = 'content.articles.create';
case MODIFY_ARTICLE = 'content.articles.update';
case REMOVE_ARTICLE = 'content.articles.delete';
}
 
class ArticleController extends Controller
{
public function index()
{
$this->authorize(ContentPermission::LIST_ARTICLES);
 
return Inertia::render('Articles/Index', [
'articles' => Article::with('author')
->latest()
->paginate(15)
]);
}
 
public function store(ArticleRequest $request)
{
$this->authorize(ContentPermission::CREATE_ARTICLE);
 
$article = Article::create(array_merge(
$request->validated(),
['author_id' => auth()->id()]
));
 
return redirect()
->route('articles.edit', $article)
->with('status', 'Article created successfully');
}
 
public function update(ArticleRequest $request, Article $article)
{
$this->authorize(ContentPermission::MODIFY_ARTICLE);
 
$article->update($request->validated());
 
event(new ArticleUpdated($article));
 
return back()->with('status', 'Article updated');
}
}

The enum support creates a more type-safe and developer-friendly approach to defining and checking permissions throughout your application.

Harris Raftopoulos photo

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

Sponsored

masteringlaravel logo
Laravel Code Review

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

Visit Laravel Code Review

The latest

View all →
Validate and Convert HEIC Images in Laravel image

Validate and Convert HEIC Images in Laravel

Read article
Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues image

Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues

Read article
Reject Unexpected Array Keys with Laravel Validation image

Reject Unexpected Array Keys with Laravel Validation

Read article
Major performance improvements & security patches for Filament v4.12 and v5.7! image

Major performance improvements & security patches for Filament v4.12 and v5.7!

Read article
Laravel Boost Project Rules: Teach Agents Your Conventions image

Laravel Boost Project Rules: Teach Agents Your Conventions

Read article
Extract an Image's Dominant Color in Laravel image

Extract an Image's Dominant Color in Laravel

Read article