How To Remove Column From Table In Laravel 10 Migration

Websolutionstuff | Apr-26-2023 | Categories : Laravel MySQL

In this article, we will see how to remove columns from a table in laravel 10 migration. Here, we will learn about drop columns from migration in laravel 10. Also, you can remove multiple columns using migration. 

In laravel 8, laravel 9, and laravel 10, you can remove a column if it exists using migration. Using laravel 10 migration you can easily manage database tables.

So, let's see how to remove a column from a table in migration, laravel 10 drop a column in a migration table, and drop a column from a table in laravel 10.

To drop a column, you may use the dropColumn method on the schema builder.

1. Remove 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('posts', function (Blueprint $table) {
            $table->dropColumn('description');
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
          
    }
}

 

2. 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('posts', function (Blueprint $table) {
            $table->dropColumn(['title','description']);
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
          
    }
}

 

3. Remove Column If Exists 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()
    {
        if (Schema::hasColumn('posts', 'description')){
  
            Schema::table('posts', function (Blueprint $table) {
                $table->dropColumn('description');
            });
        }
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
          
    }
}

 


You might also like:

Recommended Post
Featured Post
Access Denied for user 'root'@'localhost' phpMyAdmin
Access Denied for user 'root'@...

phpMyAdmin is a valuable web-based tool for managing MySQL databases, simplifying various database tasks. However, runni...

Read More

Jul-28-2023

How To Create List And Grid View Using JavaScript
How To Create List And Grid Vi...

In this article, we will see how to create a list and grid view using javascript. many times clients have requirements l...

Read More

Dec-23-2020

How to Display Validation Error Message in Laravel Vue JS
How to Display Validation Erro...

Hey folks, are you ready to make your Laravel Vue.js application even more user-friendly by implementing validation erro...

Read More

Mar-08-2024

How to Install Jenkins on Ubuntu
How to Install Jenkins on Ubun...

In today's fast-paced software development landscape, I've understood the significance of continuous integration...

Read More

Aug-07-2023