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!
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
}
If you haven't run migrations after adding the SoftDeletes
trait, do so by running the following command in your terminal:
php artisan migrate
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();
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);
}
}
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);
}
}
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);
}
}
If you want to permanently remove soft-deleted records from the database, you can use the forceDelete()
method:
$user->forceDelete();
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:
In this article, we will see how to create a custom command in laravel 9. Here we will learn about how to make cust...
Feb-14-2023
In this tutorial i will show you how to login with mobile number in laravel using Laravel Custom Auth , laravel pro...
Jun-18-2021
Hello, laravel web developers! In this article, we'll see how to create a zip archive file in laravel 11. You can cr...
Jul-17-2024
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...
Jun-11-2020