How To Drop Column From Migration in Laravel Tutorial

Share this Article
Reading Time: 4 minutes
201 Views

Inside this article we will see the concept i.e How To Drop Column From Migration in Laravel Tutorial. Article contains the classified information about Laravel Remove Column From Table In Migration.

If you are looking to find a solution i.e How To Drop Column in Laravel Using Migration then you are at the right place to learn and implement.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1: Remove Column Using Migration

Let’s create a migration file.

$ php artisan make:migration change_posts_table

It will create a file i.e xxx_change_posts_table.php inside /database/migrations folder.

<?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->dropColumn('body');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

Concept

$table->dropColumn('body');

Next, run this command.

$ php artisan migrate

Once you migrate, it will drop body column from table posts.

Example #2: Remove Multiple Column Using Migration

Again, consider the same migration file.

$ php artisan make:migration change_posts_table

It will create a file i.e xxx_change_posts_table.php inside /database/migrations folder.

<?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->dropColumn(['body', 'title']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

Concept

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

Next, run this command.

$ php artisan migrate

Once you migrate, it will drop body, title column from table posts.

Example #3: Remove Column If Exists Using Migration

Again, consider the same migration file for posts table.

$ php artisan make:migration change_posts_table

It will create a file i.e xxx_change_posts_table.php inside /database/migrations folder.

<?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()
    {
        if (Schema::hasColumn('posts', 'body')){
  
            Schema::table('posts', function (Blueprint $table) {
                $table->dropColumn('body');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

Concept

if (Schema::hasColumn('posts', 'body')){
  
  Schema::table('posts', function (Blueprint $table) {
      $table->dropColumn('body');
  });
}

Next, run this command.

$ php artisan migrate

Once you migrate, it will check if column body exists. And if Yes, then it drops that from posts table.

We hope this article helped you to learn about How To Drop Column From Migration in Laravel Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Get PHP Web Development Courses on Offer in just ₹299
Get PHP Web Development Courses on Offer in just ₹299