Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.



Get Started Now!

How to use soft deleting in Laravel

Soft deleting is a Laravel feature that allows you to hide records from your application without permanently deleting them from the database. This can be useful for a variety of reasons,

such as:

  • Keeping a record of deleted records for audit purposes
  • Giving users the ability to restore deleted records
  • Avoiding database fragmentation

To use soft deleting in Laravel, you need to:

  1. Add a deleted_at column to your database table. This column should be of type datetime and nullable.
  2. Add the SoftDeletes trait to your model.
  3. Call the delete() method on the model to soft delete it.

Soft deleting is a crucial feature in Laravel, a popular PHP framework, that allows you to “delete” records from a database while preserving their existence in the database. This feature is a powerful tool for protecting your data integrity and complying with legal or regulatory requirements. It is removed from the database permanently. Soft deleting, however, doesn’t actually erase the data but instead marks it as “deleted.” This way, the information remains in the database but is no longer visible in standard queries. Soft deleting is especially useful when you need to maintain a historical record of data changes, comply with data retention policies, or recover accidentally deleted data.

Step 1: Database Migration

In your database migration file, add a deleted_at column of type timestamp to the table you want to enable soft deleting for. This column will store the deletion timestamp for each record.

Schema::create('your_table', function (Blueprint $table) {
    // ... other columns
    $table->softDeletes(); // Adds the 'deleted_at' column
});

Step 2: Model Configuration

In your model, use the SoftDeletes trait provided by Laravel. This trait adds the necessary methods and properties to the model.

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class YourModel extends Model
{
    use SoftDeletes;
    // ...
}

That’s it! Your Laravel model is now set up for soft deleting.

Performing Soft Deletes

To soft delete a record, use the delete method on your model instance. Laravel will automatically set the deleted_at column to the current timestamp without physically removing the record from the database.

$record = YourModel::find($id);
$record->delete();

Restoring Soft Deleted Records

Soft deleted records can be restored easily. Use the restore method on a model instance to bring the record back to life.

$record = YourModel::withTrashed()->find($id); // Find the soft deleted record
$record->restore(); // Restore the record

Permanently Deleting Records

In some cases, you may want to remove soft-deleted records permanently from the database. To do this, use the forceDelete method.

$record = YourModel::withTrashed()->find($id); // Find the soft deleted record
$record->forceDelete(); // Permanently delete the record

Querying Soft Deleted Records

To retrieve soft deleted records alongside active ones, use the withTrashed method in your query:

$records = YourModel::withTrashed()->get();

To retrieve only soft deleted records, you can use the onlyTrashed method:

$records = YourModel::onlyTrashed()->get();

Related Posts

Master DevOps with the Best Free Tutorials Online

The demand for DevOps professionals is skyrocketing as organizations rapidly adopt modern development and deployment methodologies. Whether you are a beginner looking to enter the DevOps space…

Error in Laravel:”Invalid Key Supplied”

while trying to log in to your Laravel application, don’t worry. This issue is commonly related to misconfigured or missing keys for Laravel Passport’s OAuth2 authentication system….

Error in Laravel “Davmixcool\MetaManager\MetaServiceProvider Not Found”

When working on Laravel projects, developers often encounter errors during the setup or runtime process. One such error is the “Class ‘Davmixcool\MetaManager\MetaServiceProvider’ not found”, which can occur…

Discover Rewa Effortlessly with Motoshare’s Convenient Bike and Car Rentals

Rewa, the “Land of White Tigers,” offers a unique blend of historical, cultural, and natural attractions that captivate every traveler. To make exploring this charming city more…

Discover Shimoga (Shivamogga) Effortlessly with Motoshare’s Bike and Car Rentals

Nestled in the lush greenery of Karnataka, Shimoga (Shivamogga) is a haven for nature lovers and history enthusiasts. From the roaring Jog Falls to the tranquil forests…

Explore the Spiritual Charm of Mathura with Motoshare’s New Bike and Car Rental Services

Mathura, the birthplace of Lord Krishna, is a city brimming with spirituality, vibrant culture, and historical significance. To make your journey through this sacred city seamless, Motoshare…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x