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

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article
Laranon: Reversible PII Anonymization for Laravel and LLMs image

Laranon: Reversible PII Anonymization for Laravel and LLMs

Read article
PHP 8.6 is released!  image

PHP 8.6 is released!

Read article
Laravel Warrant: Row-Level Permissions With Eloquent image

Laravel Warrant: Row-Level Permissions With Eloquent

Read article
JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43 image

JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43

Read article
Durable Workflow: Long-Running Workflows for Laravel image

Durable Workflow: Long-Running Workflows for Laravel

Read article