What is Laravel Soft Delete and How Does it Work?

I’ll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method of deleting data in an application without removing it from the database.

Why do we need Soft Delete?

The application’s data is crucial, and since many users often work on the same records and accidentally erase them, you can use the soft delete option to keep deleted records in your database for as long as you need them. Soft Delete allows the admin to restore the deleted data if needed.

How Does it Work?

The Laravel implements soft deletes by using the SoftDeletes trait in your Eloquent model and adding a deleted_at column to the corresponding database table. They provide useful methods like delete() method is used to delete a record and restore() method to the restoration of data.

Implementing Soft Delete in Laravel?

Let’s start to the implementation of the Laravel soft delete. We’ll create a model and add soft delete and restore model into here. Also, explore hard delete and query to get all soft deleted records:

Step 1: We need to add a deleted_at column to the table for the model where we want to enable soft deletes, I am taking an Employee model and added deleted_at column as like below:

public function up()
{
    Schema::create('employees', function (Blueprint $table) {
        $table->id();
        $table->string('slug')->unique();            
        $table->string('name');
        $table->number('age'); 
        $table->number('salary');
        $table->timestamps();
        $table->softDeletes(); // This adds the 'deleted_at' column
   });
}

Step 2: Let’s create an Employee.php file in our model folder, I have added SoftDeletes trait into this file. Here’s an example:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Employee extends Model
{
    use SoftDeletes; // Add this line to enable soft deletes
	
	protected $fillable = ['name', 'salary', 'age'];

    protected $dates = ['deleted_at'];
}

We have implemented soft delete for employee table, So whenever a user deletes a record using Eloquent methods like delete(), Laravel will set the deleted_at timestamp, which denotes the record as “trashed” rather than permanently deleted.

How To Use?

Now, I’ll use soft delete functionality for the employee table in our Laravel controller, Let’s take a simple example.

public function deleteEmp($id)
{
    $emp = Employee::find($id);
    $emp->delete(); // This soft deletes the employee

    // Additional logic...
}

How To Restore Soft Deleted Record

We have deleted employee data in the above code, Let’s restore the deleted data in Laravel. The withTrashed() method allows us to find soft-deleted records. Here’s how we do it.

public function restoreEmployee($id)
{
    $emp = Employee::withTrashed()->find($id);
    $emp->restore(); // This restores the soft-deleted emp
}

permanently delete a record

We can also delete permanently data on the employee table which is already implemented soft delete. The $model->forceDelete() method allows to permanent delete records from the database. This method removes the record entirely from the database, including the deleted_at timestamp.

public function deleteEmployeeForever($id)
{
    // If you have not deleted before
    $emp = Employee::find($id);

    // If you have soft-deleted it before
    $emp = Employee::withTrashed()->find($id);

    $post->forceDelete(); // This permanently deletes the post for ever!
}

How To Get All deleted and non-deleted records?

The withTrashed() method helps to get both types of data: records that are deleted and trashed.

$emps = Employee::withTrashed()->get();

How To Get Trashed REcords

You can use onlyTrashed() method to get only trashed records.

$trashedPosts = Post::onlyTrashed()->get();

Conclusion

We have explored how to implement Laravel soft delete in your application. Laravel provides SoftDeletesn traits with inbuilt methods. You can use those methods to soft delete records, hard delete and fetch trashed records.

Leave a Reply

Your email address will not be published.