How to drop column only if already exists using Laravel migrations? Here is how:

First we will create custom method called dropColumnIfExists , this method we can put in Helper file:

                    public function dropColumnIfExists($myTable, $column)
    {
        if (Schema::hasColumn($myTable, $column)) 
        {
            Schema::table($myTable, function (Blueprint $table) use($column)
            {
                $table->dropColumn($column);
            });
        }

    }
                  

So now in our migration file we will include this helper method:

                    use App\Http\Controllers\Helper;

                  

and now inside of our up() method we can use it:

                    public function up()
    {
        Schema::table('users', function (Blueprint $table) {

            $helper = new Helper();
            $helper->dropColumnIfExists('users','nickname'); // remove field nickname if exists on users table
            $helper->dropColumnIfExists('users','description'); //remove field description if exists on users table
            });
    }
                  

 

So, now we are using our custom dropColumnIfExist() method which will remove table field only if exists.

Now you can run migrate method as usual:

                    php artisan migrate
                  

 

done! 👨‍💻