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 Create Custom Command In Laravel 9
How To Create Custom Command I...

In this article, we will see how to create a custom command in laravel 9. Here we will learn about how to make cust...

Read More

Feb-14-2023

Login with Mobile Number using Laravel Custom Auth
Login with Mobile Number using...

In this tutorial i will show you how to login with mobile number in laravel using Laravel Custom Auth , laravel pro...

Read More

Jun-18-2021

How to Create ZIP Archive File in Laravel 11 Example
How to Create ZIP Archive File...

Hello, laravel web developers! In this article, we'll see how to create a zip archive file in laravel 11. You can cr...

Read More

Jul-17-2024

Google Map With Draggable Marker Example
Google Map With Draggable Mark...

In this example I will show you how to add a google map to your website, In many websites you can see they have added ma...

Read More

Jun-11-2020