Removing Soft Delete from Laravel Table with Migration - Techvblogs

Removing Soft Delete from Laravel Table with Migration

Learn how to remove soft delete from Laravel table with migration easily to restore original table behavior and data handling.


Smit Pipaliya - Author - Techvblogs
Smit Pipaliya
 

6 days ago

TechvBlogs - Google News

This article explains how to get rid of the "soft delete" functionality from a table in Laravel using migrations. In Laravel, soft delete means marking data as deleted in the database instead of actually deleting it. However, sometimes this feature may not be required, and it can be removed from the table. The tutorial will walk you through the steps of removing soft delete from a Laravel table using migrations in a clear and organized way.

If you want to drop soft delete from the table using migration, then laravel provides the dropSoftDeletes() function to remove soft delete from the table.

Soft delete work with deleted_at column. You need to remove that. You can see the below solution with a complete migration example.

Solution:

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

Example:

Create new migration, run the following command:

php artisan make:migration add_soft_delete_posts

Next, update your migration file to match the following:

database/migrations/2023_02_02_135632_add_soft_delete_posts.php

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function(Blueprint $table)
        {
            $table->softDeletes();
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('posts', function(Blueprint $table)
        {
            $table->dropSoftDeletes();
        });
    }
};

Now, you can run the migration:

php artisan migrate

Conclusion

In conclusion, removing the soft delete feature from a Laravel table is a straightforward process that can be achieved through migrations. By following the steps outlined in this article, you can effectively remove the soft delete functionality from your Laravel application and modify the database structure accordingly. Whether you no longer need the soft delete feature or simply want to streamline your application, this tutorial provides a clear and easy-to-follow guide for removing soft delete from a Laravel table using migrations.

Thank you for reading this article.

Comments (0)

Comment


Note: All Input Fields are required.