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.
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.
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.
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.
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... }
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 }
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! }
The withTrashed()
method helps to get both types of data: records that are deleted and trashed.
$emps = Employee::withTrashed()->get();
You can use onlyTrashed()
method to get only trashed records.
$trashedPosts = Post::onlyTrashed()->get();
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.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More