How to check table column exist or not in Laravel


Besides the 'hasTable' static method, you also have 'hasColumn' method which can check column exist or not on database table of laravel.

So basically you can check whether a table and a column exist or not in laravel, the 'hasTable' is for checking table and 'hasColumn' for checking column.

The static method 'hasColumn' requires two parameters, the first one is the table name and the second is column name, so you need to specify the table and the column name, like this:

Schema::hasColumn('table_name', 'column_name');

Example:
if (Schema::hasColumn('users', 'email'))
{
    echo 'column email exist on table users';
} else {
    echo 'column email is not exist on table users';
}

That's it, you can put the code on your migration script or on your laravel code if needed, it's up to you.


EmoticonEmoticon