How to Remove Column from Table in Laravel Migration?

In this engaging blog post, you can explain how to remove columns from database tables in a Laravel application using migrations. The article should provide readers with a clear, step-by-step guide while keeping their interest piqued with engaging content.

I will give you some examples that way you can easily remove columns using migration. let’s see the below example that will help you.

1) Remove the Column using Migration

2) Remove Multiple Column using Migration

3) Remove Column If Exists using Migration

Remove Column using Migration

<?php

  

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

  

class ChangeslugsTableColumn extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        Schema::table('slugs', function (Blueprint $table) {

            $table->dropColumn('orgs');

        });

    }

  

    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

          

    }

}

Remove Multiple Column using Migration

<?php

  

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

  

class ChangePostsTableColumn extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        Schema::table('slugs', function (Blueprint $table) {

            $table->dropColumn(['orgs', 'title']);

        });

    }

  

    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

          

    }

}

Remove Column If Exists using Migration

<?php

  

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

  

class ChangeslugsTableColumn extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        if (Schema::hasColumn('slugs', 'orgs')){

  

            Schema::table('slugs', function (Blueprint $table) {

                $table->dropColumn('slug_id');

            });

        }

    }

  

    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

          

    }

}

I hope This will be helpful…!!!

Related Posts

Simple Guide to Digestive Health and Finding the Right Care

Stomach and gut problems can feel scary and confusing. Many people suffer in silence. Pain, bloating, or acid can disrupt daily life. Families often search online and…

Read More

How DataOps Makes Financial Analytics Faster, Safer, and Better

Introduction Have you ever stopped to think about what actually keeps a bank running? It is easy to picture vaults full of cash or gold bars, but…

Read More

Best DevOps Tools for Beginners: A Simple Guide

Starting a cloud engineering career feels hard. There are so many tools to learn. You open one video, and then ten more pop up. Soon you feel…

Read More

DataOps and AIOps Explained: How Smart Technology Saves Businesses

Introduction Imagine you are trying to build a really big puzzle, but all the pieces are scattered across different rooms in your house. Not only that, but…

Read More

A Complete Guide to DevOps Training and Consulting in Japan for Enterprise Teams

Software now changes very fast. Companies in Japan want to keep up. But many teams face a skills gap. They want to build better software. They also…

Read More

Real-World DataOps Implementations: A Simple Guide for Data Teams

Introduction Imagine a manager walks into a Monday meeting with a sales report. Another manager has a different report, and the numbers do not match. Both reports…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x