Mastering Soft Deletes in Laravel: withTrashed(), onlyTrashed(), and restore() Methods
Soft deletes are a powerful feature in Laravel that allows you to “delete” records without permanently removing them from your database. Instead of being permanently erased, the records are marked with a timestamp in the deleted_at column. This article will guide you through using withTrashed(), onlyTrashed(), and restore() methods to manage soft-deleted records effectively.
Enabling Soft Deletes in Laravel
To use soft deletes, add the SoftDeletes trait to your Eloquent model:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
}Update your migration file to include a deleted_at column:
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->softDeletes();
});
}Understanding withTrashed(), onlyTrashed(), and restore()
withTrashed()
Use withTrashed() to include both soft-deleted and non-deleted records in your query:
$allPosts = Post::withTrashed()->get();This method is useful when you need to display all records, regardless of their deletion status.
onlyTrashed()
Use onlyTrashed() to retrieve only the soft-deleted records:
$deletedPosts = Post::onlyTrashed()->get();This method is helpful when you want to display or work with only those records marked as deleted.
restore()
Use restore() to bring back soft-deleted records:
post::onlyTrashed()->where('id', $id)->restore();This method removes the deleted_at timestamp and makes the record active again.
Practical Example: Implementing Soft Deletes in a Blog
Imagine you have a blogging application where users can delete and restore posts. Here’s how you might implement these methods:
Include All Posts (with Deleted):
$allPosts = Post::withTrashed()->get();Show Only Deleted Posts:
$deletedPosts = Post::onlyTrashed()->get();Restore a Soft-Deleted Post:
$post = Post::onlyTrashed()->find($id);
$post->restore();Combining Soft Deletes with Other Queries
You can combine withTrashed() or onlyTrashed() with other query constraints:
$posts = Post::withTrashed()->where('author_id', $authorId)->get();
$deletedPosts = Post::onlyTrashed()->where('category', 'Laravel')->get();Conclusion
Soft deletes provide a flexible way to handle record deletion in Laravel. Using methods like withTrashed(), onlyTrashed(), and restore(), you can manage deleted records efficiently, giving you the ability to undo deletions and keep your data safe.
Have you used soft deletes in your Laravel projects? Share your experiences or any challenges you’ve faced in the comments below! And for more Laravel tips, consider subscribing to our newsletter!
I hope you enjoyed this post. Feel free to clap this article (👏) and subscribe to my Medium newsletter for more.
👋 I offer tech consulting services for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through LinkedIn, or you can find my email on my GitHub profile.





