Soft Deletes on Specific Tables, Not Just "Posts"
There is a common misconception in Laravel development that soft deletes are a global toggle or a feature limited to generic posts tables without specific configuration. The reality is slightly more nuanced: soft delete is a table-level trait, not a column-level magic switch that applies automatically to every model named Post. If you need this behavior for your content management system, you must explicitly apply the SoftDeletes trait to the specific model representing your posts and ensure your database schema supports it.
The source material suggests using $id->softDeletes(). This is a misunderstanding of how Laravel handles soft deletes. The method softDeletes() does not exist on Eloquent models as a runtime toggle. Instead, you define the behavior by importing the SoftDeletes trait directly into your model class definition. Once the trait is applied, Laravel automatically manages the deleted_at column during save operations if that column exists in your database.
Implementing Soft Deletes for Your Posts Table
To enable soft deletes on a Contents table (or whatever your actual model name is), you first need to ensure the underlying database structure includes a deleted_at timestamp column. You do not need to manually define this column in your Model; Eloquent will look for it automatically if the trait is present and the column exists.
If you are starting fresh with migrations, use Laravel's migration helper to add the column:
php artisan make:migration add_deleted_at_to_contents_table --table=contents
Inside the migration file generated at database/migrations/xxxx_add_deleted_at_to_contents_table.php, add the following code:
Schema::table('contents', function (Blueprint $table) {
$table->softDeletes();
});
This command generates a timestamp column named deleted_at. Once the migration runs, you can update your model. Create or edit the Contents model to include the trait:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Contents extends Model
{
use SoftDeletes;
}
With this setup, calling $contents = Contents::find($id); followed by $contents->delete(); will not remove the row from the database. Instead, it will set the deleted_at column to the current timestamp. The record remains in the table but is excluded from standard queries like Contents::all().
Querying and Restoring Soft-Deleted Records
One of the primary benefits of soft deletes is the ability to recover data without losing it permanently. However, you must be intentional about how you query these records. By default, Eloquent ignores soft-deleted rows. If a post has been "deleted," Contents::all() will simply skip it.
To retrieve a list that includes both active and deleted posts, use the withTrashed() scope:
$allPosts = Contents::withTrashed()->get(); // Returns all records, including soft-deleted ones
$activePosts = Contents::onlyTrashed()->get(); // Returns only the soft-deleted ones
Restoring a specific record is straightforward using the restore() method on the model instance. This reverses the deletion by setting the deleted_at column back to NULL:
$id = 123;
$contents = Contents::withTrashed()->findOrFail($id); // Fetch even if deleted
$contents->restore();
Note that you cannot restore a record using Contents::find($id) if the record is soft-deleted, as standard find operations ignore those rows. You must explicitly enable the trashed scope when retrieving the model instance before attempting restoration.
Configuration and Trade-offs
While powerful, soft deletes introduce specific considerations regarding your application's logic. The SoftDeletes trait relies on the presence of a deleted_at column in the table schema. If you are using Laravel 9 or later, the default behavior for new migrations includes this column when using $table->softDeletes(), but older versions require manual addition.
Additionally, consider how your API endpoints handle deletions. If an endpoint expects to return data after a delete operation, it must explicitly request soft-deleted records or handle the 404 response differently than permanent deletes. This distinction ensures that users can recover content if they realize a deletion was accidental, preserving data integrity while maintaining a clean database structure.
For advanced scenarios involving relationships or bulk operations, Laravel provides scopes like forceDeleted to check for hard deletes versus soft deletes, ensuring your logic remains robust regardless of the underlying storage state.
