Soft delete is a technique used in database management systems that involves marking a record or row as deleted, rather than physically deleting it from the database. This is typically done to preserve data integrity and enable easy recovery of deleted data. Soft delete is particularly useful in situations where you need to keep track of the history of changes made to your database.

In Laravel, the soft delete functionality is built into the Eloquent ORM (Object-Relational Mapping) system. When you implement soft delete in Laravel, you essentially instruct Eloquent to mark a record as deleted by setting a special deleted_at column in the database table. This deleted_at column stores a timestamp of when the record was marked as deleted.

When you retrieve records from the database using Eloquent, by default it will only return records that are not soft deleted. This means that any record with a non-null deleted_at value will not be included in the results. However, you can retrieve soft deleted records by using the withTrashed method or the onlyTrashed method.

The withTrashed method returns all records, including soft deleted ones, while the onlyTrashed method returns only the soft deleted records. You can restore a soft deleted record by calling the restore method on the Eloquent model instance. This sets the deleted_at column to null, restoring the record to its original state.

Soft delete is useful in many different scenarios. For example, it can be used to implement a “trash” feature in a content management system, allowing users to restore accidentally deleted content. It can also be used to keep track of changes to a database over time, or to maintain a record of data that has been archived but is still needed for reference purposes.

Laravel has built-in support for soft deletes. Here are the steps to implement soft delete in Laravel:

Step 1: Add SoftDeletes trait to your model Add the Illuminate\Database\Eloquent\SoftDeletes trait to your model to enable soft deleting.

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

class YourModel extends Model
{
    use SoftDeletes;
}

Step 2: Add deleted_at column to your table Add a deleted_at column to the table that corresponds to the model. This column will be used to track deleted records.

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

Step 3: Perform soft delete To soft delete a record, simply call the delete method on the model instance.

$yourModelInstance->delete();

This will set the deleted_at column to the current timestamp, marking the record as “deleted”.

Step 4: Retrieve soft deleted records To retrieve soft deleted records, use the withTrashed method on the model’s query builder.

$softDeletedRecords = YourModel::withTrashed()->get();

This will retrieve all records, including soft deleted records. To retrieve only soft deleted records, use the onlyTrashed method.

$onlySoftDeletedRecords = YourModel::onlyTrashed()->get();

Step 5: Restore soft deleted records To restore a soft deleted record, call the restore method on the model instance.

$yourModelInstance->restore();

This will set the deleted_at column to null, restoring the record to its original state.

That’s it! By following these steps, you can easily implement soft delete in your Laravel application.

Soft delete in Laravel example

here’s an example of implementing soft delete in Laravel:

Let’s assume you have a users table in your database, and you want to implement soft delete for this table. First, you’ll need to add a deleted_at column to the table:

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
    $table->softDeletes(); // add the deleted_at column for soft delete
});

Next, you’ll need to use the SoftDeletes trait in your User model:

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

class User extends Model
{
    use SoftDeletes;

    //...
}

With the SoftDeletes trait, the User model now has the necessary functionality to perform soft delete. To delete a user, you can simply call the delete() method on the model instance:

$user = User::find(1);
$user->delete();

This will mark the user with an id of 1 as soft deleted by setting the deleted_at column to the current timestamp.

To retrieve soft deleted records, you can use the withTrashed() method on the query builder:

$softDeletedUsers = User::withTrashed()->where('name', 'John')->get();

This will return all users with the name “John”, including any that have been soft deleted.

To retrieve only soft deleted records, you can use the onlyTrashed() method:

$softDeletedUsers = User::onlyTrashed()->where('name', 'John')->get();

This will return only users with the name “John” that have been soft deleted.

To restore a soft deleted record, you can use the restore() method on the model instance:

$user = User::onlyTrashed()->find(1);
$user->restore();

This will set the deleted_at column to null, restoring the user with an id of 1 to its original state.

That’s it! With these few steps, you can easily implement soft delete in Laravel for any table in your database.

Why soft delete is an important feature ?

Soft delete is an important feature in Laravel for several reasons:

  1. Data Integrity: Soft delete helps maintain data integrity by preserving records that have been deleted from the database. Instead of permanently deleting records, soft delete marks them as deleted by setting the deleted_at column value to a timestamp. This allows you to restore accidentally deleted records or recover important data that has been deleted.
  2. Auditing: Soft delete helps with auditing by maintaining a historical record of changes made to the database. You can use this record to see who deleted a particular record, when it was deleted, and if it was restored.
  3. Compliance: Soft delete helps organizations meet compliance requirements by maintaining a record of data that has been deleted. This is important for organizations that need to maintain records for regulatory compliance or legal reasons.
  4. User Experience: Soft delete can provide a better user experience by allowing users to recover accidentally deleted data. This is especially useful in content management systems where users may accidentally delete important content.
  5. Performance: Soft delete is often faster and less resource-intensive than hard delete because it does not require deleting the data physically from the database. Instead, it sets a flag to mark the record as deleted.

In summary, soft delete is an important feature in Laravel that helps maintain data integrity, auditing, compliance, user experience, and performance. By using soft delete, you can recover accidentally deleted data, maintain a historical record of changes, meet compliance requirements, and provide a better user experience for your users.

When & where to use soft delete

Soft delete is a useful feature in Laravel that can be used in various scenarios. Here’s a case study of when to use soft delete:

Suppose you are developing a web application for a hospital. The application has a database table for patient records, which includes sensitive medical information. The hospital is required to keep patient records for a certain period of time, but after that, they can be deleted to comply with data protection regulations.

In this scenario, you can use soft delete to maintain the integrity of the patient records while complying with data protection regulations. Instead of deleting patient records physically from the database, you can mark them as deleted using the deleted_at column. This way, you can recover accidentally deleted records and maintain a historical record of changes made to the patient records.

Furthermore, if the hospital wants to comply with the “right to be forgotten” regulation, they can use soft delete to delete patient records after a certain period of time while keeping a record of the deletion. This way, the hospital can show regulatory authorities that they are complying with data protection regulations while protecting the privacy of their patients.

In summary, soft delete is a useful feature in Laravel that can be used in scenarios where you need to maintain data integrity, comply with data protection regulations, and protect the privacy of your users. In the case study above, soft delete was used to maintain the integrity of patient records while complying with data protection regulations.

Conclusion:

Soft delete is a valuable tool for developers working with databases in Laravel. It can help to streamline the database management process, provide better data management capabilities, and help meet regulatory requirements. By taking advantage of soft delete, developers can create more robust and reliable web applications with ease.

Categorized in: