Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Adds support for enums on mapInto collection method #51027

Merged
merged 4 commits into from
Apr 12, 2024
Merged

[11.x] Adds support for enums on mapInto collection method #51027

merged 4 commits into from
Apr 12, 2024

Conversation

lukeraymonddowning
Copy link
Contributor

@lukeraymonddowning lukeraymonddowning commented Apr 11, 2024

Hey all!

This PR adds backed enum support to the Collection::mapInto method. This essentially allows us to build up enums from an array of backing values.

A Sample Use Case

One use case for this is when dealing with an array of data passed from the front end.

public function store(Request $request)
{
    $request->validate([
        'features' => ['array'],
        'features.*' => [new Enum(Feature::class)],
    ]);

    $features = $request
        ->collect('features')
        ->mapInto(Feature::class);

    if ($features->contains(Feature::DarkMode)) {
        // 🌙
    }
}

Imagine that we have a Feature enum, and the frontend provides checkboxes for selecting from these features. Obviously, we can't send the enum case in the request, so we send the backing value of the enum instead. Using the updated mapInto method, we can easily transform those backing values into enum cases.

There we go, nothing major. Just felt it was a nice DX improvement and in my mind is what I'd expect when passing an enum to the mapInto method.

Thanks for all the hard work!

Kind Regards,
Luke

@lukeraymonddowning lukeraymonddowning changed the title [11.x] Adds support for enums on mapInto collection method. [11.x] Adds support for enums on mapInto collection method Apr 11, 2024
@taylorotwell taylorotwell merged commit 321da34 into laravel:11.x Apr 12, 2024
28 checks passed
@lukeraymonddowning lukeraymonddowning deleted the 11.x_map_into_enum branch April 12, 2024 12:10
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