How to Restore Soft Deleted Data in Laravel (with Hidden Tricks)

This guide covers laravel soft delete restore, laravel trashed data restore, laravel withTrashed, onlyTrashed, and the restore method. Recover soft deleted records and restore deleted records easily with Laravel.

How to Restore Soft Deleted Data in Laravel (with Hidden Tricks) Image

Learn how to restore soft deleted data in Laravel using Eloquent. This guide covers laravel soft delete restore, laravel trashed data restore, laravel withTrashed, onlyTrashed, and the restore method. Recover soft deleted records and restore deleted records easily with Laravel.

In real-world applications, data recovery is a common need. Whether it's restoring a mistakenly deleted user or bringing back archived blog posts, Laravel makes this easy with its Soft Delete feature.

In this post, you'll learn how to restore soft deleted data in Laravel, explore withTrashed(), onlyTrashed(), and the restore() method, and see real examples of how to use them effectively.

What is Soft Delete in Laravel?

Laravel’s Soft Delete functionality allows you to “delete” a record without actually removing it from the database. Instead, it sets a timestamp in a special deleted_at column.

This is incredibly useful when you want to keep data temporarily removed, yet still have the option to recover soft deleted records later.

How to Enable Soft Deletes in Laravel

To use soft deletes, first update your model:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
    use SoftDeletes;
}

Then, update your migration file to include the deleted_at column:

Schema::table('posts', function (Blueprint $table) {
    $table->softDeletes();
});

Run the migration if needed:

php artisan migrate

Soft Deleting a Record

Here’s how you soft delete a record:

$post = Post::find(1);
$post->delete(); // sets 'deleted_at' instead of removing the record 

How to Restore Soft Deleted Records in Laravel

Now, let’s talk about restoring soft deleted data in Laravel.

Restore a Single Soft Deleted Record

To restore a single record:

Post::withTrashed()->find($id)->restore();
  • withTrashed() includes trashed (soft deleted) records.

  • restore() revives the deleted record by setting deleted_at back to null.

Restore All Trashed Records

If you want to restore all soft deleted records:

Post::onlyTrashed()->restore();

This is useful when performing bulk restore operations.

Restore Records with Condition

You can also restore trashed data based on conditions:

Post::onlyTrashed()->where('status', 'draft')->restore();

This restores only soft deleted posts that are drafts.


Displaying Soft Deleted Records

You might want to list soft deleted records in your admin panel or dashboard.

Show only trashed records:

$trashedPosts = Post::onlyTrashed()->get();

Include soft deleted records in normal query:

$allPosts = Post::withTrashed()->get();

Permanently Delete Soft Deleted Records

If you want to completely remove soft deleted data, use forceDelete():

Post::onlyTrashed()->where('id', $id)->forceDelete();

⚠️ Be careful! This removes the record from the database permanently.


createOrRestore() Method (Undocumented)

Laravel has an undocumented method called createOrRestore(), which is part of the Illuminate\Database\Eloquent\Model class, and it works with soft deleted records. The method combines the functionality of creating a new record or restoring an existing soft-deleted record in a single operation.

How createOrRestore() Works:

This method is essentially a convenience for handling situations where you may want to restore a trashed record if it already exists in the database but is soft deleted, or create a new record if it doesn’t exist at all.

Usage Example:

use App\Models\User;
// Attempt to create or restore a user $user = User::createOrRestore(['email' => 'john@example.com', 'name' => 'John Doe']);

In this example:

  • If a soft-deleted user exists with that email, the method will restore that user.

  • If the user doesn’t exist, it will create a new record with the provided attributes.

Important Note:

  • This method is not documented officially in the Laravel docs, so its use might be considered a bit risky in production apps, as it could be subject to change in future Laravel versions.

  • It only works with soft-deleted records and tries to restore them if the attributes match.

When to Use It:

  • You need to reconcile records and don't want to have duplicates of soft-deleted data.

  • It's great when you want to ensure data consistency without checking the deletion status manually.

 

Summary

Here’s a quick recap on how to restore soft deleted records in Laravel:

FunctionPurpose
softDeletes()Enables soft delete on a model
withTrashed()Includes trashed records in query
onlyTrashed()Fetches only trashed records
restore()Restores soft deleted record(s)
forceDelete()Permanently deletes a record

Laravel provides everything you need to restore trashed data securely and efficiently.

Do you Like?