How to Get Soft Deleted Records in Laravel 10

Websolutionstuff | Nov-27-2023 | Categories : Laravel

Hey there! Ever wondered how to recover deleted data in Laravel 10 without much hassle? Well, you're in luck! I'm here to guide you through the simple process of getting soft-deleted records. Soft deletes allow us to mark records as deleted without actually removing them from the database, offering a flexible way to manage data.

So, let's dive in and learn step by step how to effortlessly retrieve those soft-deleted gems in our Laravel 8, Laravel 9 and Laravel 10 application.

Ready? Let's get started on this data recovery journey!

Step 1: Implement Soft Deletes in Your Model

Make sure your model uses the SoftDeletes trait. Open the corresponding model file, typically located in the app/Models directory, and add the use SoftDeletes statement at the top:

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

class User extends Model
{
    use SoftDeletes;

    // ... other model code
}

 

Step 2: Run Migrations

If you haven't run migrations after adding the SoftDeletes trait, do so by running the following command in your terminal:

php artisan migrate

 

Step 3: Soft Delete Records

In your application logic, when you want to delete a record softly (i.e., mark it as deleted without removing it from the database), use the delete() method on your model:

$user = User::find($id);
$user->delete();

 

Step 4: Retrieve Soft-Deleted Records using withTrashed()

To retrieve soft-deleted records, you can use the withTrashed() method along with your query. For example:

<?php
   
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use App\Models\User;
   
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $softDeletedRecords = User::withTrashed()->get();
  
        dd($softDeletedRecords);
    }
}

 

 

Example: Retrieve Soft-Deleted Records using onlyTrashed()

This will fetch all records, including the soft-deleted ones. If you want only the soft-deleted records, you can use the onlyTrashed() method:

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $onlySoftDeletedRecords = User::onlyTrashed()->get();
  
        dd($onlySoftDeletedRecords);
    }
}

 

Step 5: Restore Soft-Deleted Records

If you want to restore a soft-deleted record, you can use the restore() method on the model instance:

<?php
   
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use App\Models\User;
   
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $restoreData = User::restore();
  
        dd($restoreData);
    }
}

 

Step 6: Permanently Delete Records

If you want to permanently remove soft-deleted records from the database, you can use the forceDelete() method:

$user->forceDelete();

 

Conclusion:

And that's it! You've now successfully implemented and used soft deletes in Laravel 10, allowing you to retrieve and manage soft-deleted records in your application. Also, you can use in Laravel 8, and Laravel 9 Applications.

 


You might also like:

Recommended Post
Featured Post
Carbon Add Hours In Laravel
Carbon Add Hours In Laravel

In this article, we will see examples of carbon add hours in laravel 8. Carbon provides many functions like ad...

Read More

Dec-09-2020

How To Use Google Recaptcha V2 In Laravel 9
How To Use Google Recaptcha V2...

In this article, we will see how to use google ReCaptcha v2 in laravel 9. Google ReCaptcha is used for advance...

Read More

Jun-14-2022

How To Add jQuery Datatable Column Filter Dropdown On Top
How To Add jQuery Datatable Co...

In this article, we will see how to add a jquery datatable column filter dropdown on top. Here, we will learn how t...

Read More

Jan-19-2023

Laravel 9 Socialite Login with Facebook Account
Laravel 9 Socialite Login with...

In this article, we will see laravel 9 socialite login with a facebook account. Many websites provide different typ...

Read More

Apr-16-2022