Search code examples
laravellaravel-7user-managementsoft-delete

How to Soft Delete a data in Laravel 7 but keeping the data with a Disable status in application?


I have a table with user management in my Laravel Application, and I start use soft delete to dont lost data from database, but I want when I delete my data from my app, just put a disable status instead of delete from app.

enter image description here

Someone can help me? thanks!


Solution

  • When you use soft deletes, it just added a time stamp to the deleted_at column. So when you retrieve the data, Laravel then just provide data that doesnt have timestamp on the column. If you want to retrieve all of the data (including soft deleted data) then just use withTrashed() on your modal.

    $flights = App\Flight::withTrashed()
                ->where('account_id', 1)
                ->get();
    

    So on your front end, just do a simple checking by disable the button if the deleted_at column is not null.