Soft Deletes — Trash & Restore
Soft deletes mark records as deleted without removing them from the database. A deleted_at timestamp is set instead. Soft-deleted records are automatically excluded from queries. You can restore them or permanently delete them later — like a recycle bin.
Soft Deletes
// Migration — add soft delete column
$table->softDeletes(); // Adds deleted_at column
// Model — use SoftDeletes trait
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
}
// Regular operations (soft-deleted posts are hidden)
Post::all(); // Only non-deleted posts
Post::find(1)->delete(); // Sets deleted_at (still in DB)
// Include soft-deleted
Post::withTrashed()->get(); // All posts including deleted
Post::onlyTrashed()->get(); // Only deleted posts
// Restore a soft-deleted post
$post = Post::withTrashed()->find(1);
$post->restore();
// Permanently delete
$post->forceDelete();
// Check if soft-deleted
if ($post->trashed()) {
echo 'This post is in the trash';
}Tip
Tip
Practice Soft Deletes Trash Restore in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Route::resource generates all 7 CRUD routes with one line
Practice Task
Note
Practice Task — (1) Write a working example of Soft Deletes Trash Restore from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Soft Deletes Trash Restore is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.