Enhanced Enum Processing with Laravel's Default Parameter Support

Last updated on by

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

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

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

Visit Acquaint Softtech
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

The latest

View all →
Laravel Quota: Usage Budgets for Calendar Periods image

Laravel Quota: Usage Budgets for Calendar Periods

Read article
Find the Security Vulnerabilities in Your Laravel App with Sensagraph image

Find the Security Vulnerabilities in Your Laravel App with Sensagraph

Read article
Uvora: A macOS Menu Bar App for Finding Laravel Projects image

Uvora: A macOS Menu Bar App for Finding Laravel Projects

Read article
Passwordless Sign-In with Fortify Two-Factor Support in Laravel image

Passwordless Sign-In with Fortify Two-Factor Support in Laravel

Read article
Intercept: Middleware Guardrails for Laravel AI Agents image

Intercept: Middleware Guardrails for Laravel AI Agents

Read article
AI Review for Laravel Upgrades image

AI Review for Laravel Upgrades

Read article