Today we are going to see in this tutorial how to use soft delete in Laravel and what are its benefits.
Soft delete, also known as logical deleting or virtual deleting, is a technique used in software development to mark records as deleted rather than physically deleting them from a database. This approach allows the possibility of recovering deleted data or auditing changes made to the system. In the context of the question, let us discuss how soft deleting can be implemented in a Laravel application.
Laravel is a popular PHP framework that provides built-in support for soft deleting records through its Eloquent ORM (object-relational mapping) system. Eloquent makes it easy to perform database operations by doing away with the underlying SQL queries.
Benefits of Soft Deleting:
- Data Recovery: Soft deleting allows you to retain deleted records in the database, making it possible to recover them if needed.
- Auditing and History: Soft deleting provides a way to track and analyze changes made to the system, including the ability to restore or view historical data.
- Data Integrity: Soft deleting preserves referential integrity by keeping related records intact, even if a parent record is deleted.
- User Experience: Soft deleting can provide a better user experience by allowing users to undo accidental deletions or restore their accounts.
- Compliance and Regulation: Soft deleting aligns with data retention policies and regulatory requirements by maintaining deleted records for auditing purposes.
To enable soft deleting in a Laravel application, follow these steps:
Database Migration
you need to create a migration file to add a “deleted_at” column to the table you want to enable soft deleting for. The “deleted_at” column will store the timestamp when a record is soft deleted. In this tutorial, we are managing the user account for soft delete.
⫸ Create a migration file to add the deleted_at
column to the users
table:
php artisan make:migration add_deleted_at_to_users --table=users
⫸ Open the generated migration file and modify the up()
method:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->timestamp('deleted_at')->nullable();
});
}
⫸ Run the migration:
php artisan migrate
Implementing Soft Deleting in the User Model:
- Open the
User
model located inapp/Models/User.php
(orapp/User.php
for older Laravel versions). - Add the
SoftDeletes
trait and update the class definition:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
// ...
}
By including the SoftDeletes
trait, Laravel will automatically handle soft deleting operations for your model.
Soft Delete and Restore Records
You can now perform soft delete and restore operations on the model instances.
To soft delete a record, you can simply call the delete()
method on an instance of your model:
$record = User::find($id);
$record->delete();
The delete()
method will set the deleted_at column to the current timestamp.
To restore a soft-deleted user, use the restore()
method:
$user = User::withTrashed()->find($id);
$user->restore();
Soft Deleted and Non-deleted Records:
Laravel provides several methods for querying soft-deleted records:
withTrashed():
Includes soft-deleted records.onlyTrashed():
Retrieves only soft-deleted records.withoutTrashed():
Excludes soft-deleted records.
Here are some examples:
// Retrieve all users (including soft-deleted)
$users = User::withTrashed()->get();
// Retrieve only soft-deleted users
$users = User::onlyTrashed()->get();
// Retrieve non-deleted users
$users = User::withoutTrashed()->get();
By implementing soft deleting in your Laravel application, you can easily manage data deletions, maintain data integrity, and have the flexibility to recover or analyze deleted records when necessary.