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

tinkerwell logo
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article