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 Install PHP Soap Extension in Ubuntu 23.04
How to Install PHP Soap Extens...

Hey fellow developers! Today, let's tackle the installation of the PHP SOAP extension on our Ubuntu 23.04 systems. I...

Read More

Jan-31-2024

What Is Digital Marketing?
What Is Digital Marketing?

Digital marketing is the component of marketing that utilizes internet and online bas...

Read More

Oct-19-2021

How to Upload Multiple Image in Laravel 8
How to Upload Multiple Image i...

In this example we will see how to upload multiple image in laravel 8. here, we wil see tutorial of multiple image uploa...

Read More

Sep-17-2021

File Upload With Progress Bar In Angular 13
File Upload With Progress Bar...

In this article, we will see the file upload with a progress bar in angular 13. In this example, we will learn how to im...

Read More

Jun-11-2022