Add Soft Delete Functionality
Introduction
In software development, it's common to encounter scenarios where records need to be temporarily removed or hidden from view, but not permanently deleted from the database. This is where the concept of soft delete comes into play. In this article, we'll explore how to implement soft delete functionality in the Quantum PHP framework, allowing records to be "soft deleted" rather than completely removed from the database.
Database Schema Update
To implement soft delete functionality, we need to update the database schema to include a new deleted_at
column in relevant tables. This column will store the timestamp when a record was soft deleted.
Step 1: Add deleted_at column to database tables
We'll add a new deleted_at
column with a TIMESTAMP data type and make it nullable, allowing it to hold a NULL value indicating a non-deleted record.
ALTER TABLE users
ADD COLUMN deleted_at TIMESTAMP NULL DEFAULT NULL;
ALTER TABLE products
ADD COLUMN deleted_at TIMESTAMP NULL DEFAULT NULL;
Step 2: Update database schema to include deleted_at column
We'll update the database schema to include the deleted_at
column in the relevant tables.
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class AddDeletedAtColumn extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->timestamp('deleted_at')->nullable();
});
Schema::table('products', function (Blueprint $table) {
$table->timestamp('deleted_at')->nullable();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('deleted_at');
});
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('deleted_at');
});
}
}
Model Updates
Next, we'll update the base model class to include methods for soft deleting and restoring records.
Step 1: Implement softDelete() method
We'll create a softDelete()
method that sets the deleted_at
field to the current timestamp.
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
protected $dates = ['deleted_at'];
public function softDelete()
{
$this->update(['deleted_at' => now()]);
}
}
Step 2: Implement restore() method
We'll create a restore()
method that sets the deleted_at
field back to NULL.
public function restore()
{
$this->update(['deleted_at' => null]);
}
Step 3: Implement trashed() method
We'll create a trashed()
method to check if a record is deleted (i.e., if deleted_at
is not NULL).
public function trashed()
{
return $this->deleted_at !== null;
}
Query Scope for Soft Deletes
To automatically exclude "soft-deleted" records from query results, we'll add a global scope to models.
Step 1: Add global scope to models
We'll create a global scope that excludes "soft-deleted" records from query results.
use Illuminate\Database\Eloquent\Builder;
class BaseModel extends Model
{
protected static function boot()
{
parent::boot();
static::addGlobalScope('deleted', function (Builder $builder) {
$builder->where('deleted_at', null);
});
}
}
Step 2: Implement withTrashed() and onlyTrashed() methods
We'll create methods like withTrashed()
and onlyTrashed()
to allow query customization to retrieve soft-deleted records if needed.
public function withTrashed()
{
return $this->withoutGlobalScope('deleted');
}
public function onlyTrashed()
{
return $this->withoutGlobalScope('deleted')->where('deleted_at', '!=', null);
}
Controller/Service Layer
We'll update the controller/service layer to handle soft delete and restore actions.
Step 1: Update controller/service layer to handle soft delete and restore actions
We'll update the controller/service layer to handle soft delete and restore actions correctly.
use App\Models\BaseModel;
class UserController extends Controller
{
public function softDelete($id)
{
$user = BaseModel::find($id);
$user->softDelete();
}
public function restore($id)
{
$user = BaseModel::find($id);
$user->restore();
}
}
Testing
We'll write unit and integration tests for the soft delete functionality.
Step 1: Write unit tests for soft delete functionality
We'll write unit tests to ensure that the soft delete functionality works correctly.
use Tests\TestCase;
use App\Models\BaseModel;
class SoftDeleteTest extends TestCase
{
public function test_soft_delete()
{
$user = new BaseModel();
$user->save();
$user->softDelete();
$this->assertNotNull($user->deleted_at);
}
public function test_restore()
{
$user = new BaseModel();
$user->save();
$user->softDelete();
$user->restore();
$this->assertNull($user->deleted_at);
}
}
Step 2: Write integration tests for soft delete functionality
We'll write integration tests to ensure that the soft delete functionality works correctly in a real-world scenario.
use Tests\TestCase;
use App\Models\BaseModel;
class SoftDeleteIntegrationTest extends TestCase
{
public function test_soft_delete_and_restore()
{
$user = new BaseModel();
$user->save();
$user->softDelete();
$this->assertNotNull($user->deleted_at);
$user->restore();
$this->assertNull($user->deleted_at);
}
}
Introduction
In our previous article, we explored how to implement soft delete functionality in the Quantum PHP framework. Soft delete allows records to be "soft deleted" rather than completely removed from the database. In this article, we'll answer some frequently asked questions about implementing soft delete functionality in Quantum PHP framework.
Q: What is soft delete functionality?
A: Soft delete functionality allows records to be "soft deleted" rather than completely removed from the database. This means that the record is not physically deleted from the database, but rather marked as deleted with a timestamp.
Q: Why do I need soft delete functionality?
A: Soft delete functionality is useful when you need to temporarily remove records from view, but still want to keep them in the database for auditing or other purposes. It's also useful when you need to implement a "recycle bin" feature in your application.
Q: How do I implement soft delete functionality in Quantum PHP framework?
A: To implement soft delete functionality in Quantum PHP framework, you need to follow these steps:
- Add a
deleted_at
column to the relevant tables in your database. - Update the base model class to include methods for soft deleting and restoring records.
- Add a global scope to models that automatically excludes "soft-deleted" records from query results.
- Implement methods like
withTrashed()
andonlyTrashed()
to allow query customization to retrieve soft-deleted records if needed. - Update the controller/service layer to handle soft delete and restore actions.
- Write unit and integration tests for the soft delete functionality.
Q: What are the benefits of soft delete functionality?
A: The benefits of soft delete functionality include:
- Temporarily removing records from view without physically deleting them from the database.
- Keeping records in the database for auditing or other purposes.
- Implementing a "recycle bin" feature in your application.
- Reducing the risk of data loss due to accidental deletion.
Q: What are the challenges of implementing soft delete functionality?
A: The challenges of implementing soft delete functionality include:
- Adding a new column to the database schema.
- Updating the base model class to include methods for soft deleting and restoring records.
- Implementing a global scope to models that automatically excludes "soft-deleted" records from query results.
- Writing unit and integration tests for the soft delete functionality.
Q: How do I troubleshoot issues with soft delete functionality?
A: To troubleshoot issues with soft delete functionality, you can:
- Check the database schema to ensure that the
deleted_at
column is correctly added. - Verify that the base model class is correctly updated to include methods for soft deleting and restoring records.
- Check the global scope to ensure that it is correctly implemented to exclude "soft-deleted" records from query results.
- Write unit and integration tests to ensure that the soft delete functionality is working correctly.
Q: Can I use soft delete functionality with other features in Quantum PHP framework?
A: Yes, you can use soft delete functionality with other features in Quantum PHP framework, such as:
- Eloquent models: Soft delete functionality can be used with Eloquent models to temporarily remove records from view.
- Query builder: Soft delete functionality can be used with the query builder to exclude "soft-deleted" records from query results.
- Controllers and services: Soft delete functionality can be used with controllers and services to handle soft delete and restore actions.
By following these steps and answering these frequently asked questions, you can successfully implement soft delete functionality in the Quantum PHP framework and take advantage of its benefits.