diff --git a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php index e170451465c9..39744e9178bb 100644 --- a/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php +++ b/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php @@ -2,6 +2,7 @@ namespace Illuminate\Routing; +use BackedEnum; use Illuminate\Support\Arr; trait CreatesRegularExpressionRouteConstraints @@ -70,7 +71,12 @@ public function whereUuid($parameters) */ public function whereIn($parameters, array $values) { - return $this->assignExpressionToParameters($parameters, implode('|', $values)); + return $this->assignExpressionToParameters( + $parameters, + collect($values) + ->map(fn ($value) => $value instanceof BackedEnum ? $value->value : $value) + ->implode('|') + ); } /** diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 9171119f0741..cec7d2371131 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -12,6 +12,8 @@ use PHPUnit\Framework\TestCase; use Stringable; +include_once 'Enums.php'; + class RouteRegistrarTest extends TestCase { /** @@ -942,6 +944,19 @@ public function testWhereInRegistration() } } + public function testWhereInEnumRegistration() + { + $this->router->get('/posts/{category}')->whereIn('category', CategoryBackedEnum::cases()); + + $invalidRequest = Request::create('/posts/invalid-value', 'GET'); + $this->assertFalse($this->getRoute()->matches($invalidRequest)); + + foreach (CategoryBackedEnum::cases() as $case) { + $request = Request::create('/posts/'.$case->value, 'GET'); + $this->assertTrue($this->getRoute()->matches($request)); + } + } + public function testGroupWhereNumberRegistrationOnRouteRegistrar() { $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+'];