How to check table exist or not in Laravel


Is it possible to check existence of database table in laravel code? the answer is yes, it is possible to check whether a table already exist or not in laravel.

In laravel we can use the schema class to find out if a given table name is exist or not, there is a static method called 'hasTable', this static method will return true if the given table name is exist otherwise it will return false.

Schema::hasTable('table_name')

Example:
if (Schema::hasTable('users'))
{
    echo 'table exist';
} else {
    echo 'table not exist';
}

You can put this on your migration script or inside your application code if needed, the same code works on both laravel 4 and laravel 5.


EmoticonEmoticon