How to Get Soft Deleted Records in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Drop Soft Delete from Table using Laravel Migration?
- Laravel Add Soft Delete to Existing Table Example
- Get Array of Ids from Eloquent Models in Laravel
- How to Use Limit and Offset in Laravel Eloquent?
- How to Find Multiple Ids using Laravel Eloquent?
- How to Select Specific Columns in Laravel Eloquent Model?
- Laravel 9 Eloquent Mutators and Accessors Example
- Laravel Eloquent without() and withOnly() Method Example
- Laravel Eloquent doesntHave() Condition Example
- How to Restore Deleted Records in Laravel?
- Laravel Unique Validation With Soft Delete Example
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- How to use Soft Delete in Laravel?
- Laravel Eloquent Inner Join with Multiple Conditions Example