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 →
Laravel Starter Kits Now Ship with Vite+ image

Laravel Starter Kits Now Ship with Vite+

Read article
Pessimistic Locking in Laravel Eloquent with refreshForUpdate() image

Pessimistic Locking in Laravel Eloquent with refreshForUpdate()

Read article
Mask Query Bindings in Laravel Exception Messages image

Mask Query Bindings in Laravel Exception Messages

Read article
whereBinary(): Case-Sensitive MySQL Queries in Laravel image

whereBinary(): Case-Sensitive MySQL Queries in Laravel

Read article
Compile PHP to Native Binaries with TypePHP image

Compile PHP to Native Binaries with TypePHP

Read article
State of Laravel 2026 Survey Is Now Open image

State of Laravel 2026 Survey Is Now Open

Read article