Skip to content

[12.x] Add a default option when retrieving an enum from data#55735

Merged
taylorotwell merged 2 commits into
laravel:12.xfrom
elbojoloco:add-default-option-enum
May 14, 2025
Merged

[12.x] Add a default option when retrieving an enum from data#55735
taylorotwell merged 2 commits into
laravel:12.xfrom
elbojoloco:add-default-option-enum

Conversation

@elbojoloco

Copy link
Copy Markdown
Contributor

Enables you to provide a default value when retrieving an enum from data which is consistent with other getters in the InteractsWithData trait An example use case is when you're handling GET params in a dashboard.

Old:

$chartType = request()->enum('chart_type', ChartTypeEnum::class) ?: ChartTypeEnum::Bar;

New:

$chartType = request()->enum('chart_type', ChartTypeEnum::class, ChartTypeEnum::Bar);

@shaedrich

Copy link
Copy Markdown
Contributor

Would be cool to have an attribute on the enum or a method or the like 🤔

@taylorotwell taylorotwell merged commit fc0c66f into laravel:12.x May 14, 2025
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants