Laravel — P55: Soft Deleted Models in Routes

Route to trashed records with confidence

Running through a tutorial series like this isn’t as straightforward as it seems. You want to cover topics in a specific order and yet each time you get to a new topic, you realize that you need to cover something else in order to do it. For example, after going through some of the easier topics in Laravel, I figured we could tackle some of the topics on their own, like Routes, Models, Controllers, etc.

I’m in somewhat of dilemma right now. The last topic of the route series that I wanted to cover is how to include soft-deleted models. The only issue is, we haven’t covered how to soft-delete a model just yet. Do we cover it now, and then cover it again later? Or do I skip this article and get back to routes later?

This is the type of thought process that goes in my head each time I write an article. After taking the time to dissect this one as well, I figured that it’s not that out there for you to use your imagination a little until we get to soft-deletions.

https://www.dinocajic.com/laravel-p54-route-groups-middleware-and-controllers/

What does it mean to soft-delete a model?

Typically when you remove a record from the database, it does just that, it removes it. When you enable soft-deletions, the table gets a deleted_at column which just stores a timestamp of when the record was deleted. We’ll cover how to create those types of models later (hint: you do it in the migration files).

How do you recover records that have been soft deleted?

When you return a collection from the database, Laravel will return only the records that are active. That means that if the record has been completely removed or soft-deleted, it will not appear in the collection. But what if you wanted for it to appear in the collection as well? This can only be done if the record has been soft-deleted. If soft-deletion is enabled on a particular model, you can use the withTrashed method.

use App\Models\User;
 
Route::get('/users/{user}', function (User $user) {
    return $user;
})->withTrashed();

In the example above, we’re type-hinting a User instance and are searching for the user, even if that user has been soft-deleted. If there’s a match, the user will be returned.

I think we’ve covered routes as far as we can. Hopefully we don’t have to return to them later.

https://github.com/dinocajic/youtube-laravel

Laravel Series

Continue your Laravel Learning.

Laravel — P54: Route Groups — Middleware and Controllers

Organize routes, secure them, and keep controllers lean

Laravel – P54: Route Groups Middleware & Controllers

In part 54 of our Laravel series, unlock the power of route groups to apply middleware stacks and shared controller namespaces in one stroke. Minimize repetition, harden security, and keep your routes file tidy as your API grows.

Laravel — P55: Soft Deleted Models in Routes

Route to trashed records with confidence

Laravel – P55: Routes Soft – Deleted Models

In part 55 of our Laravel series, learn how to work with soft-deleted Eloquent models inside routes. Configure bindings to include trashed records, build restore endpoints, and prevent 404 errors when referencing archived data.

 

Laravel — P56: Dependency Injection in Controllers

Inject services, unclutter your controllers

Laravel – P56: Dependency Injection In Controllers

In part 56 of our Laravel series, deep-dive into leveraging the framework’s service container directly inside controllers. See how constructor and method injection reduce boilerplate, improve testability, and keep your business logic sharply focused.

Leave a Reply