Introduction:

Accidentally deleting important data in a Laravel application can indeed be a significant issue. It can lead to data loss, create hurdles in the application’s functionality, and cause frustration for users who may have to deal with the aftermath. Moreover, retracing the steps to understand the reason behind the deletion can be time-consuming and challenging. It’s crucial to implement robust data backup and recovery strategies to mitigate the risks associated with accidental data deletion.

Laravel, one of the most popular PHP frameworks, offers robust features for handling database operations efficiently. Among its many functionalities, Laravel provides soft deletes, a feature that allows you to “delete” records from the database without actually removing them permanently.

What are Soft Deletes?

In Laravel, soft deletes offer a mechanism for managing data deletion without permanently removing records from the database. Soft deletes in Laravel mark a record row as “deleted” by setting a flag on the row. This approach enables the data to stay in the database, preserving referential integrity, while being excluded from regular queries.

Soft delete offers several advantages:

1. Prevents accidental data loss:
By marking records as “deleted” rather than physically removing them from the database, soft delete helps prevent accidental data loss. This means that even if a record is mistakenly deleted, it can still be recovered easily.
2. Maintains database integrity:
Soft delete allows you to maintain referential integrity within the database. Since the deleted records still exist but are simply flagged as deleted, relationships between tables remain intact, ensuring the overall integrity of the database structure.
3. Flexible data management:
Soft delete provides flexibility in managing data. It allows you to temporarily remove records from regular queries without permanently deleting them. This flexibility can be particularly useful in scenarios where data needs to be temporarily hidden or archived.

How Do Soft Deletes Work in Laravel?

Laravel simplifies the implementation of soft deletes through its built-in features. By adding a deleted_at timestamp column to your database table, Laravel can automatically manage soft deletes for you. When you call the delete() method on an Eloquent model, Laravel sets the deleted_at column to the current timestamp, effectively marking the record as deleted. Conversely, when you retrieve records, Laravel automatically filters out those with a non-null deleted_at value, ensuring they don’t appear in your query results.

Enabling Soft Deletes in Laravel:

To enable soft deletes for a model in Laravel, you need to do the following:

1. Add deleted_at column: using migration, add a new column deleted_at to the column where you want to enable the soft deletes.

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

2. Ensure that your corresponding database table has a deleted_at column of type timestamp or datetime, which should allow null values.
3. Add the SoftDeletes trait to your model class.

 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\SoftDeletes;
 class Post extends Model
 {
   use SoftDeletes;

   protected $dates = ['deleted_at'];
 }

Usage of Soft Deletes in Laravel:

Soft Deleting Records:

1. Once you’ve configured your model for soft deletes, you can start using them in your application. To soft delete a record, simply call the delete() method on an instance of the model.

$post = Post::find($id);
$post->delete();

Restoring Soft Deleted Records:

2. Laravel provides a convenient method for restoring soft deleted records. You can use the restore() method on a model instance to undo the soft delete operation.

 $post = Post::withTrashed()->find($id);
 $post->restore();

Querying Soft Deleted Records:

3. You can use the withTrashed() method to include soft deleted records in your results.

  $posts = Post::withTrashed()->get();    

Similarly, you can use the onlyTrashed() method to retrieve only soft deleted records.

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

Conclusion:

Laravel’s soft delete feature is a handy tool for managing deletions in your application. Instead of fully removing records, it marks them as deleted, keeping your database organized and preserving important data. Whether you’re working on a basic blog or a big project, soft deletes help maintain data integrity and track changes effectively.

Support On Demand!

                                         
Laravel