Skip to content

[9.x] Allow using backed enums as route parameters - #43294

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
Neol3108:enum-route-params
Jul 20, 2022
Merged

[9.x] Allow using backed enums as route parameters#43294
taylorotwell merged 1 commit into
laravel:9.xfrom
Neol3108:enum-route-params

Conversation

@Neol3108

Copy link
Copy Markdown
Contributor

This PR adds support for using backed enum values as route parameters.

Example/Reasoning

I have a project where I have 2 models, Model1 and Model2. Where Model1 hasMany Model2.
Each Model1 can have one Model2 for each backed enum case. In my DB there is a unique constraint on model1_id and enum_value.
Then I have a route /model1/{model1}/model2/{model2:enum_value} (scoped).

This works fine, except every time I want to generate an URL from a named route I have to specify $model2->enum_value->value instead of just $model2.

Breaking changes

I can't think of any, except for if you expect an Object of class <YourBackedEnum> could not be converted to string error. Correct me if I'm wrong tho.

@taylorotwell
taylorotwell merged commit 3cb5557 into laravel:9.x Jul 20, 2022
@Neol3108
Neol3108 deleted the enum-route-params branch July 21, 2022 17:16
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.

2 participants