Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

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
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi
Shift logo

Shift

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

Shift
Tinkerwell logo

Tinkerwell

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

Tinkerwell
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
PhpStorm logo

PhpStorm

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

PhpStorm
Lucky Media logo

Lucky Media

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

Lucky Media
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
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
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
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

The latest

View all →
Typed Objects for Eloquent with Expressive image

Typed Objects for Eloquent with Expressive

Read article
Malware Blocking and Dependency Policies in Composer 2.10 image

Malware Blocking and Dependency Policies in Composer 2.10

Read article
Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects image

Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects

Read article
Playa: Cookie-Based Temporary Players for Laravel image

Playa: Cookie-Based Temporary Players for Laravel

Read article
Scheduler Attributes and Listener Discovery Control in Laravel 13.12.0 image

Scheduler Attributes and Listener Discovery Control in Laravel 13.12.0

Read article
The PHP Foundation Launches an Ecosystem Security Team image

The PHP Foundation Launches an Ecosystem Security Team

Read article