Skip to content

Commit

Permalink
Fix trashed implicitBinding with child with no softdelete (#41282) (#…
Browse files Browse the repository at this point in the history
…41814)

(cherry picked from commit b8be411)

Co-authored-by: Jori Stein <44996807+stein-j@users.noreply.github.com>
  • Loading branch information
BrandonSurowiec and stein-j committed Apr 4, 2022
1 parent b4b39be commit 8ca1471
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/ImplicitRouteBinding.php
Expand Up @@ -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';

Expand Down
29 changes: 29 additions & 0 deletions tests/Integration/Routing/ImplicitRouteBindingTest.php
Expand Up @@ -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']);
Expand Down

0 comments on commit 8ca1471

Please sign in to comment.