Everything you need as a full stack developer

Eloquent Soft Deletes with use SoftDeletes trait

- Posted in Laravel by

TL;DR Laravel's SoftDeletes trait enables "deleting" records without removing them from the database by adding a flag/column to indicate deletion status. The trait can be added to models, requiring a 'deleted_at' timestamp column in the database table.

Eloquent Soft Deletes with Use SoftDeletes Trait

As a Laravel developer, you're likely familiar with Eloquent's powerful ORM capabilities. But have you ever encountered situations where you need to delete data without actually deleting it? Perhaps you want to maintain a record of deleted items or allow users to recover accidentally deleted content.

This is where soft deletes come in – a feature that enables you to "delete" records while still keeping them in the database. In this article, we'll explore how to use Laravel's SoftDeletes trait to implement soft deleting in your applications.

What are Soft Deletes?

Soft deletes allow you to mark a record as deleted without actually removing it from the database. This is achieved by adding a flag or column to indicate whether an item has been "deleted". When a record is marked for deletion, its corresponding value in this flag/column is set to 1 (or some other indicator), effectively hiding the record from views and queries.

The SoftDeletes Trait

Laravel provides a trait called SoftDeletes that simplifies the implementation of soft deleting. To use it, you need to add it to your model file by adding the following line at the top:

use Illuminate\Database\Eloquent\SoftDeletes;

Next, include the trait in your model's class definition:

class YourModel extends Model {
    use SoftDeletes;
}

Configuring Soft Deletes

Once you've added the SoftDeletes trait to your model, you need to configure it. Laravel requires a date column named deleted_at to store the timestamp when an item was deleted.

To add this column, run the following migration:

Schema::table('your_table_name', function (Blueprint $table) {
    $table->softDeletes();
});

This will create a deleted_at timestamp in your database table. You can customize the date format using the $dates property on your model.

protected $dates = [
    'created_at',
    'updated_at',
    'deleted_at'
];

Using Soft Deletes

Now that you've set up soft deleting, you can use it in your controllers and queries. When an item is deleted using the delete method, its corresponding value in the deleted_at column will be updated.

YourModel::find(1)->delete();

To retrieve all items, including those marked for deletion:

YourModel::withTrashed()->get();

Conversely, to retrieve only active (i.e., not deleted) items:

YourModel::withoutTrashed()->get();

Conclusion

Soft deletes are a powerful feature in Laravel that allows you to maintain records of deleted items while keeping your database clean. With the SoftDeletes trait and a few configuration tweaks, you can easily implement soft deleting in your applications.

In this article, we've covered the basics of soft deletes, including how to add the SoftDeletes trait to your model and configure it to work with your database table. Whether you're working on a large-scale application or a small project, understanding soft deletes will help you build more robust and scalable systems.

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more