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 →
Building EasyReply: How We Used Laravel to Unify Customer Support image

Building EasyReply: How We Used Laravel to Unify Customer Support

Read article
PostgreSQL Monitoring and Schema Linting for Laravel with Vacuum image

PostgreSQL Monitoring and Schema Linting for Laravel with Vacuum

Read article
PayZephyr: One Payment API for Stripe, Paystack, and PayPal image

PayZephyr: One Payment API for Stripe, Paystack, and PayPal

Read article
Bifrost Turns One With AI Builds and New Workflows image

Bifrost Turns One With AI Builds and New Workflows

Read article
Preview Blade Templates in macOS Finder with Quick Blade image

Preview Blade Templates in macOS Finder with Quick Blade

Read article
Artisan Debugging Commands in Laravel Telescope 5.24.0 image

Artisan Debugging Commands in Laravel Telescope 5.24.0

Read article