From 8ca1471235944025c4863be4795279d729a693f0 Mon Sep 17 00:00:00 2001 From: Brandon Surowiec Date: Mon, 4 Apr 2022 11:12:55 -0400 Subject: [PATCH] Fix trashed implicitBinding with child with no softdelete (#41282) (#41814) (cherry picked from commit b8be411c27ae9f0ef822dab0c1e6c48beb3e06e1) Co-authored-by: Jori Stein <44996807+stein-j@users.noreply.github.com> --- .../Routing/ImplicitRouteBinding.php | 2 +- .../Routing/ImplicitRouteBindingTest.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/ImplicitRouteBinding.php b/src/Illuminate/Routing/ImplicitRouteBinding.php index 413f56bd8948..1dbc5720fd82 100644 --- a/src/Illuminate/Routing/ImplicitRouteBinding.php +++ b/src/Illuminate/Routing/ImplicitRouteBinding.php @@ -43,7 +43,7 @@ public static function resolveForRoute($container, $route) : 'resolveRouteBinding'; if ($parent instanceof UrlRoutable && ($route->enforcesScopedBindings() || array_key_exists($parameterName, $route->bindingFields()))) { - $childRouteBindingMethod = $route->allowsTrashedBindings() + $childRouteBindingMethod = $route->allowsTrashedBindings() && in_array(SoftDeletes::class, class_uses_recursive($instance)) ? 'resolveSoftDeletableChildRouteBinding' : 'resolveChildRouteBinding'; diff --git a/tests/Integration/Routing/ImplicitRouteBindingTest.php b/tests/Integration/Routing/ImplicitRouteBindingTest.php index 233ffbbd54f7..56311b55975b 100644 --- a/tests/Integration/Routing/ImplicitRouteBindingTest.php +++ b/tests/Integration/Routing/ImplicitRouteBindingTest.php @@ -142,6 +142,35 @@ public function testEnforceScopingImplicitRouteBindings() $response->assertNotFound(); } + public function testEnforceScopingImplicitRouteBindingsWithTrashedAndChildWithNoSoftDeleteTrait() + { + $user = ImplicitBindingUser::create(['name' => 'Dries']); + + $post = $user->posts()->create(); + + $user->delete(); + + config(['app.key' => str_repeat('a', 32)]); + Route::scopeBindings()->group(function () { + Route::get('/user/{user}/post/{post}', function (ImplicitBindingUser $user, ImplicitBindingPost $post) { + return [$user, $post]; + })->middleware(['web'])->withTrashed(); + }); + + $response = $this->getJson("/user/{$user->id}/post/{$post->id}"); + $response->assertOk(); + $response->assertJson([ + [ + 'id' => $user->id, + 'name' => $user->name, + ], + [ + 'id' => 1, + 'user_id' => 1, + ], + ]); + } + public function testEnforceScopingImplicitRouteBindingsWithRouteCachingEnabled() { $user = ImplicitBindingUser::create(['name' => 'Dries']);