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] Support soft deleted models when using explicit route model binding #51651

Merged
merged 3 commits into from
May 31, 2024

Conversation

gbradley
Copy link
Contributor

This PR allows the resolution of soft-deleted models when using Laravel's explicit route-model binding feature.

It allows developers to use explicit route-model binding without having to customise the resolution logic when dealing with soft-deleted models. To do so it uses the same withTrashed() method as implicit binding.

Before:

Route::get('/users/{user}', ...);

Route::bind('user', function (string $value) {
    return User::where('id', $value)->withTrashed()->firstOrFail();
});

After:

Route::get('/users/{user}', ...)->withTrashed();

Route::model('user', User::class);

This introduces no breaking changes, as resolveSoftDeletableRouteBinding is only being called when the route explicitly requests withTrashed() and the corresponding model uses the SoftDeletes trait.

@Rizky92
Copy link

Rizky92 commented May 31, 2024

I think we already have it...
https://laravel-code.tips/you-can-call-withtrashed-on-a-route-to-accept-soft-deleted-models/

What's the difference with this method?

@gbradley
Copy link
Contributor Author

@Rizky92 That example is using implicit binding. Currently, withTrashed doesn’t work with explicit binding; this PR makes the behaviour consistent.

@driesvints driesvints changed the title [11.x] Support soft deleted models when using explicit route model binding. [11.x] Support soft deleted models when using explicit route model binding May 31, 2024
@taylorotwell taylorotwell merged commit 4ebbabe into laravel:11.x May 31, 2024
27 of 28 checks passed
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.

3 participants