close

Check If a Column Exists in Laravel Migration File

Hello Guys, How are you all? Hope You all Are Fine. Today we are talking about Check If a Column Exists in Laravel Migration File in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Check If a Column Exists in Laravel Migration File Error Occurs?

Today we are talking about Check If a Column Exists in Laravel Migration File in php.

How To Solve Check If a Column Exists in Laravel Migration File Error ?

  1. How To Solve Check If a Column Exists in Laravel Migration File Error ?

    To Solve Check If a Column Exists in Laravel Migration File Error You could make your own 'dropColumnIfExists()' function that checks the column existence then drop it:

  2. Check If a Column Exists in Laravel Migration File

    To Solve Check If a Column Exists in Laravel Migration File Error You could make your own 'dropColumnIfExists()' function that checks the column existence then drop it:

Solution 1


You could make your own ‘dropColumnIfExists()’ function that checks the column existence then drop it:

function myDropColumnIfExists($myTable, $column)
{
    if (Schema::hasColumn($myTable, $column)) //check the column
    {
        Schema::table($myTable, function (Blueprint $table)
        {
            $table->dropColumn($column); //drop it
        });
    }

}

And use it on ‘down()’ function like this:

public function down()
{
    myDropColumnIfExists('table_one', 'column_two');
    myDropColumnIfExists('table_one', 'column_one');
}

Solution 2

You need something just like this

  public function down()
    {
        if (Schema::hasColumn('users', 'phone'))
        {
            Schema::table('users', function (Blueprint $table)
            {
                $table->dropColumn('phone');
            });
        }
    }

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment