Laravel Soft deleting


Soft deleting in Laravel is a feature that allows you to "delete" records from the database without permanently removing them. Instead of actually deleting the record, Laravel marks it as deleted by setting a timestamp in a designated column (usually deleted_at). This enables you to restore the record later if needed, providing a safer way to handle deletions.

Key Concepts of Soft Deleting

  1. Migration Setup: To use soft deletes, you need to add a deleted_at timestamp column to your database table. You can do this using Laravel's migrations.

  2. Model Setup: Your Eloquent model needs to use the SoftDeletes trait, which provides the necessary methods for handling soft deletes.

  3. Soft Delete Operations: Instead of permanently deleting a record, you can mark it as deleted. You can also retrieve soft-deleted records or restore them if needed.

Step-by-Step Implementation

1. Migration Setup

To enable soft deletes, you need to add a deleted_at column to your table. You can do this in a migration file. Here's an example:

use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); $table->softDeletes(); // Add this line for soft deletes }); } public function down() { Schema::dropIfExists('posts'); } }

Run the migration to update the database:

php artisan migrate

2. Model Setup

Next, use the SoftDeletes trait in your Eloquent model:

namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Post extends Model { use SoftDeletes; // Include the SoftDeletes trait protected $dates = ['deleted_at']; // Specify the deleted_at attribute as a date }

3. Soft Delete Operations

Now that you’ve set up soft deletes, you can perform various operations:

  • Soft Delete a Record:

To soft delete a record, you can use the delete method:

$post = Post::find(1); $post->delete(); // Soft deletes the record
  • Retrieve Non-Deleted Records:

By default, when you query the model, soft-deleted records are excluded:

$posts = Post::all(); // Only retrieves non-deleted posts
  • Retrieve Soft-Deleted Records:

You can use the withTrashed method to include soft-deleted records in your query:

$allPosts = Post::withTrashed()->get(); // Retrieves all posts, including soft-deleted ones
  • Restore Soft-Deleted Records:

To restore a soft-deleted record, you can use the restore method:

$post = Post::withTrashed()->find(1); // Find the soft-deleted post $post->restore(); // Restores the soft-deleted post
  • Force Delete a Record:

If you want to permanently delete a record (bypassing soft deletes), you can use the forceDelete method:

$post = Post::withTrashed()->find(1); $post->forceDelete(); // Permanently deletes the record

Summary

In summary, soft deleting in Laravel provides a safe and flexible way to manage deletions:

  • Non-Permanent Deletions: Records are marked as deleted without being removed from the database.
  • Easy Restoration: Soft-deleted records can be restored easily when needed.
  • Control Over Visibility: Use methods like withTrashed and onlyTrashed to manage the visibility of soft-deleted records.

This feature is particularly useful in applications where accidental deletions can be problematic or when you want to maintain a history of records.