avatarBinu Mathew

Summary

The web content provides a comprehensive guide on implementing and managing soft deletes in Laravel using the withTrashed(), onlyTrashed(), and restore() methods, allowing for efficient handling of record deletion and recovery.

Abstract

The article titled "Mastering Soft Deletes in Laravel: withTrashed(), onlyTrashed(), and restore() Methods" delves into the intricacies of soft deletes within the Laravel framework. It explains how to enable soft deletes by adding the SoftDeletes trait to an Eloquent model and modifying the migration file to include a deleted_at column. The author guides readers through the usage of withTrashed() to retrieve all records including soft-deleted ones, onlyTrashed() to select only the soft-deleted records, and restore() to undelete records and make them active again. Practical examples are provided, such as implementing these methods in a blogging application to manage deleted posts. The article emphasizes the flexibility that soft deletes offer, enabling developers to handle data persistence more safely and efficiently. It also encourages readers to share their experiences with soft deletes and to subscribe for more Laravel tips. Additionally, the author offers tech consulting services for Laravel applications, inviting contact through LinkedIn or GitHub.

Opinions

  • The author believes that soft deletes are a powerful feature in Laravel for managing record deletion without permanent data loss.
  • Soft deletes are presented as a beneficial tool for applications where data recovery is important, such as a blogging platform.
  • The author suggests that combining soft delete methods with other query constraints can enhance data management capabilities.
  • The article implies that using soft deletes is a best practice for handling deletions in Laravel applications.
  • By offering consulting services, the author positions themselves as an expert in the field, ready to assist with Laravel application development and optimization.
  • The invitation to subscribe to a newsletter and engage with the content through comments indicates the author's commitment to building a community and fostering ongoing learning and discussion around Laravel development practices.

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.

Photo by Debby Hudson on Unsplash

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.

Laravel
Laravel Framework
Laravel Eloquent
PHP
MySQL
Recommended from ReadMedium