ItSolutionStuff.com

How to Get Soft Deleted Records in Laravel?

By Hardik Savani • November 5, 2023
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Drop Soft Delete from Table using Laravel Migration?

Read Now →

Laravel Add Soft Delete to Existing Table Example

Read Now →

Get Array of Ids from Eloquent Models in Laravel

Read Now →

How to Use Limit and Offset in Laravel Eloquent?

Read Now →

How to Find Multiple Ids using Laravel Eloquent?

Read Now →

How to Select Specific Columns in Laravel Eloquent Model?

Read Now →

Laravel 9 Eloquent Mutators and Accessors Example

Read Now →

Laravel Eloquent without() and withOnly() Method Example

Read Now →

Laravel Eloquent doesntHave() Condition Example

Read Now →

How to Restore Deleted Records in Laravel?

Read Now →

Laravel Unique Validation With Soft Delete Example

Read Now →

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now →

How to use Soft Delete in Laravel?

Read Now →

Laravel Eloquent Inner Join with Multiple Conditions Example

Read Now →