Everything you need as a full stack developer

Eloquent Restore with restoring soft deleted models

- Posted in Laravel by

TL;DR When using Eloquent's soft delete feature in Laravel, you can restore deleted models by calling the restore() method on the affected instance, which resets the deleted_at column to NULL. This effectively un-hides the record and makes it visible again in your database.

Eloquent Restore: Restoring Soft Deleted Models in Laravel

As a developer, you're likely familiar with the concept of soft deletes in Eloquent, Laravel's ORM system. But have you ever needed to restore a soft deleted model? Perhaps your user accidentally deleted an important record and needs it back? Or maybe you've implemented a delete functionality that also keeps track of deleted records for auditing purposes.

Whatever the reason, restoring soft deleted models is a crucial aspect of database management in Laravel. In this article, we'll dive into the world of Eloquent restore, exploring how to properly restore soft deleted models and what's happening behind the scenes.

Understanding Soft Deletes

Before we begin, let's quickly recap how soft deletes work in Eloquent. When you use the delete() method on a model, it doesn't actually remove the record from your database. Instead, it updates a few columns to mark the record as deleted:

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $softDelete = true;
}

In this example, any time you call delete() on an instance of the User model, Eloquent will update the deleted_at column with the current timestamp. This effectively "hides" the record from view, making it seem like it's been deleted.

Restoring Soft Deleted Models

Now that we've covered soft deletes, let's talk about restoring them. To restore a soft deleted model, you can use the restore() method on the affected instance:

$user = User::where('name', 'John')->first();
$user->delete(); // Mark as deleted

// Later...
$user->restore(); // Restore to its original state

When you call restore(), Eloquent will simply reset the deleted_at column back to NULL. This effectively "unhides" the record, making it visible again in your database.

Behind the Scenes: What's Happening with restore()

So what's happening behind the scenes when you call restore()? Let's take a peek at the code:

public function restore()
{
    if (! $this->hasBeenSoftDeleted()) {
        return;
    }

    // Reset deleted_at column to NULL
    $this->update(['deleted_at' => null]);
}

As you can see, restore() checks whether the model has been marked as soft deleted using the hasBeenSoftDeleted() method. If it hasn't, nothing happens.

Next, it simply updates the deleted_at column to NULL, effectively restoring the record to its original state.

Conclusion

In this article, we've explored the concept of Eloquent restore and how to properly restore soft deleted models in Laravel. Whether you're implementing a delete functionality or need to recover accidentally deleted records, understanding the ins and outs of restore() is crucial for any developer working with Eloquent.

Remember: when it comes to database management, it's essential to know what's happening behind the scenes. With this knowledge, you'll be well-equipped to handle even the most complex data restoration scenarios in Laravel.

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more