Using Soft Deletes with Custom Columns in Laravel 13
Laravel 13 still supports Eloquent’s SoftDeletes trait. It allows records to be marked as deleted without physically removing them from the database.
This is especially useful when migrating older projects that need to preserve existing records.
1. Enable Soft Deletes
Import the trait into your model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class VcodeBlacklist extends Model
{
use SoftDeletes;
}
By default, Laravel expects the table to contain a nullable deleted_at column.
For a new table, add it in the migration:
$table->softDeletes();
2. Use a Custom Deleted Column
If an existing table uses a different column name, such as delete_time, define the DELETED_AT constant:
class VcodeBlacklist extends Model
{
use SoftDeletes;
const DELETED_AT = 'delete_time';
}
Laravel will use delete_time instead of deleted_at.
Note that an integer flag such as is_deleted = 0/1 is not directly compatible with the built-in SoftDeletes implementation. Such a schema requires a custom query scope or another deletion strategy.
3. Handle Legacy Timestamp Columns
If the existing table does not contain created_at and updated_at, you can disable automatic timestamps:
public $timestamps = false;
This does not prevent SoftDeletes from managing its deletion column.
If the deletion column stores Unix timestamps instead of standard date strings, configure the model’s date format according to the database schema:
protected $dateFormat = 'U';
Make sure the database column type and the application’s timestamp representation are compatible.
4. Common Soft Delete Operations
Laravel provides several methods for working with soft-deleted records:
// Soft delete a record.
$item->delete();
// Normal queries exclude soft-deleted records.
VcodeBlacklist::where('target', $phone)->exists();
// Include soft-deleted records.
VcodeBlacklist::withTrashed()->get();
// Restore a soft-deleted record.
$item->restore();
// Permanently delete a record.
$item->forceDelete();
Conclusion
The SoftDeletes trait remains available in Laravel 13 and can work with existing database tables.
When migrating a legacy project, pay particular attention to the deletion column name, timestamp fields, and the format used to store dates.
