How to implement soft delete in Laravel?

Photo by Sam Pak on Unsplash

How to implement soft delete in Laravel?

Implementing soft delete in Laravel allows you to "softly" delete records from your database by marking them as deleted instead of physically removing them. This can be useful for scenarios where you want to retain data for auditing or potential restoration. Laravel provides built-in support for soft deletes using Eloquent models.

By using the SoftDeletes trait, Laravel will automatically handle soft deletions for this model.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class YourModel extends Model
    use SoftDeletes;