Laravel Tutorials

Enhanced Enum Processing with Laravel's Default Parameter Support

Published
Enhanced Enum Processing with Laravel's Default Parameter Support image

Laravel's request enum handling has been improved with built-in default value support, streamlining enum validation and fallback logic in your applications.

Previously, handling missing or invalid enum values required additional code to provide fallbacks. The updated enum method now accepts a third parameter for default values:

$reportFormat = request()->enum('format', ReportFormat::class, ReportFormat::PDF);

$sortDirection = request()->enum('direction', SortDirection::class, SortDirection::Ascending);

This enhancement reduces boilerplate code and makes enum handling more declarative throughout your application logic.

class ReportController extends Controller
{
    public function generateSalesReport(Request $request)
    {
        $configuration = [
            'output_format' => $request->enum('format', OutputFormat::class, OutputFormat::Excel),
            'time_range' => $request->enum('range', TimeRange::class, TimeRange::LastMonth),
            'detail_level' => $request->enum('detail', DetailLevel::class, DetailLevel::Summary),
            'export_type' => $request->enum('export', ExportType::class, ExportType::Download)
        ];

        return $this->buildReport($configuration);
    }

    public function configureNotifications(Request $request)
    {
        $settings = [
            'delivery_method' => $request->enum('method', DeliveryMethod::class, DeliveryMethod::Email),
            'priority_level' => $request->enum('priority', Priority::class, Priority::Normal),
            'frequency' => $request->enum('frequency', Frequency::class, Frequency::Weekly),
            'format_type' => $request->enum('format', NotificationFormat::class, NotificationFormat::HTML)
        ];

        $this->saveNotificationSettings($settings);
        
        return response()->json(['status' => 'updated', 'settings' => $settings]);
    }

    private function buildReport(array $config)
    {
        $reportBuilder = new SalesReportBuilder();
        
        return $reportBuilder
            ->setFormat($config['output_format'])
            ->setTimeRange($config['time_range'])
            ->setDetailLevel($config['detail_level'])
            ->setExportType($config['export_type'])
            ->generate();
    }
}

The default parameter feature eliminates conditional logic and makes enum-based request handling more predictable and maintainable across your Laravel applications.

Harris Raftopoulos photo

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

Sponsored

laravelcloud logo
Laravel Cloud

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

Visit Laravel Cloud

The latest

View all →
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
Session Replay for Laravel: Self-Hosted Browser Recordings image

Session Replay for Laravel: Self-Hosted Browser Recordings

Read article
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article