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
How To Upload Large CSV File Using Queue In Laravel 9
How To Upload Large CSV File U...

In this article, we will see how to upload a large CSV file using queue in laravel 9. Here we will learn large numb...

Read More

Sep-16-2022

Github And Git Commands
Github And Git Commands

GitHub, Inc. is a United States-based global company that provides hosting for software development and version control...

Read More

Jul-15-2020

How To Import CSV File In MySQL Using Node.js
How To Import CSV File In MySQ...

In this tutorial we will see how to import CSV file in MySQL using Node.js. Import and export CSV/EXCEL file in Nod...

Read More

Jul-30-2021

How to Create Newsletter in Laravel 10
How to Create Newsletter in La...

Hey there! In this article, I'm thrilled to guide you through the exciting world of creating newsletters in Laravel...

Read More

Nov-29-2023