namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; // Important: Add this trait class Post extends Model { use SoftDeletes; // Enable soft deletes for this model // Define fillable or guarded properties as usual protected $fillable = ['title', 'content']; // The 'deleted_at' column will be automatically managed by the SoftDeletes trait. // Make sure your database table has a 'deleted_at' column (timestamp nullable). // Example migration: $table->softDeletes(); } // Usage: $post = App\Models\Post::find(1); $post->delete(); // This will set 'deleted_at' timestamp, not remove the record // Retrieve only non-deleted posts $activePosts = App\Models\Post::all(); // Retrieve all posts, including soft deleted ones $allPosts = App\Models\Post::withTrashed()->get(); // Retrieve only soft deleted posts $deletedPosts = App\Models\Post::onlyTrashed()->get(); // Restore a soft deleted post $post->restore(); // Permanently delete a post $post->forceDelete();