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

Custom route binding for backed enums doesn't work #51514

Closed
Lukasss93 opened this issue May 20, 2024 · 0 comments · Fixed by #51525
Closed

Custom route binding for backed enums doesn't work #51514

Lukasss93 opened this issue May 20, 2024 · 0 comments · Fixed by #51525
Assignees
Labels

Comments

@Lukasss93
Copy link

Laravel Version

11.7.0

PHP Version

8.3.3

Database Driver & Version

No response

Description

We're using a custom route binding for a backed enum in our Laravel application.

To avoid having to create a new backed enum (string) for plural words, we've been using a custom route binding to resolve the enum from plural words.

For single words, it works fine. But for plural words, it doesn't work anymore, giving the following error:

Object of class App\Enums\YourEnumHere could not be converted to string

⚠️ See the "Steps To Reproduce" section below for more details.

Possible Solution

Do not implicitly resolve backed enums when there is a custom route binding.

Steps To Reproduce

  1. Create a backed enum
<?php

namespace App\Enums;

enum Fruit:string
{
    case APPLE = 'apple';
    case BANANA = 'banana';
    case PEAR = 'pear';

    public static function fromPlural(string $plural): self
    {
        return match ($plural) {
            'apples' => self::APPLE,
            'bananas' => self::BANANA,
            'pears' => self::PEAR,
        };
    }
}
  1. Add custom route binding to the AppServiceProvider.php
Route::bind('fruits', fn(string $x) => Fruit::fromPlural($x));
  1. Add a route to the web.php file
Route::get('shop/get/{fruit}/{id}', function (App\Enums\Fruit $fruit, string $id) {
    dd($fruit, $id); // IT WORKS!!!
});

Route::get('shop/list/{fruits}', function (App\Enums\Fruit $fruits) {
    dd($fruits); // ERROR: Object of class App\Enums\Fruit could not be converted to string
});
@Lukasss93 Lukasss93 changed the title Custom route binding for backed enums doesn't work anymore Custom route binding for backed enums doesn't work May 20, 2024
crynobone added a commit that referenced this issue May 21, 2024
fixes #51514

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
taylorotwell pushed a commit that referenced this issue May 21, 2024
fixes #51514

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
CAAHS pushed a commit to CAAHS/laravel-framework that referenced this issue May 27, 2024
Port a078b1a (Fixes explicit route binding with `BackedEnum` (laravel#51525),
2024-05-21 crynobone) to 10.x (from 11.x).

fixes laravel#51583
refs laravel#51514

Signed-off-by: CAAHS <CAAHS@example.net>
CAAHS pushed a commit to CAAHS/laravel-framework that referenced this issue May 27, 2024
Port a078b1a (Fixes explicit route binding with `BackedEnum` (laravel#51525),
2024-05-21 crynobone) to 10.x (from 11.x).

fixes laravel#51583
refs laravel#51514

Signed-off-by: CAAHS <CAAHS@example.net>
taylorotwell pushed a commit that referenced this issue May 27, 2024
Port a078b1a (Fixes explicit route binding with `BackedEnum` (#51525),
2024-05-21 crynobone) to 10.x (from 11.x).

fixes #51583
refs #51514

Signed-off-by: CAAHS <CAAHS@example.net>
Co-authored-by: CAAHS <CAAHS@example.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants