How to Get Soft Deleted Records in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hey Friends,

This is a short guide on how to get soft deleted data in laravel. we will help you to give an example of how to restore soft deleted data in laravel. I explained simply about laravel get soft deleted records. you can see laravel get soft deleted data.

Laravel Eloquent provide soft deleted feature is awesome that way laravel excluded all soft delete record. So By default Laravel Eloquent excludes all the soft deleted records from query results.

Example 1: Laravel Get Soft Deleted Records using onlyTrashed()

You can get only soft deleted row using onlyTrashed() of Laravel Eloquent.

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Item;

class ItemController extends Controller

{

/**

* Display a listing of the resource.

*/

public function index()

{

$data = Item::onlyTrashed()->get();

dd($data);

}

}

Example 2: Laravel Get Soft Deleted Records using withTrashed()

But you can get also soft deleted record using withTrashed() of Laravel Eloquent. It will return all record from table.

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Item;

class ItemController extends Controller

{

/**

* Display a listing of the resource.

*/

public function index()

{

$data = Item::withTrashed()->get();

dd($data);

}

}

Example 3: Laravel Soft Deleted Records Restore

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Item;

class ItemController extends Controller

{

/**

* Display a listing of the resource.

*/

public function index()

{

$data = Item::onlyTrashed()->restore();

dd($data);

}

}

I hope it can help you...

Tags :
Shares