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
Laravel 9 Yajra Datatable Example
Laravel 9 Yajra Datatable Exam...

In this artical we will see laravel 9 yajra datatable example. As we all used datatable on our backend side project, Her...

Read More

Mar-10-2022

Generate Dynamic Sitemap in Laravel
Generate Dynamic Sitemap in La...

Here we will see how to generate dynamic sitemap in laravel. As we know sitemap is very important part of seo, sitemap i...

Read More

Jul-05-2021

How to install TeamViewer in Ubuntu 22.04
How to install TeamViewer in U...

Hey there, Ubuntu users! If you've ever needed a hand troubleshooting your friend's computer from the comfort of...

Read More

Jan-17-2024

How to Send Bulk Mail Using Queue in Laravel 9
How to Send Bulk Mail Using Qu...

In this article, we will see how to send bulk mail using queue in laravel 9. Laravel queue is used for sending bulk...

Read More

Mar-15-2022