Helper Helper - 2 months ago 23
PHP Question

Schema Builder : Create Table if not exists

I am using below code in Schema Builder to create table.

Schema::create('tblCategory', function (Blueprint $table) {
$table->increments('CategoryID');
$table->string('Category', 40);
$table->unique('Category', 'tblCategory_UK_Category');
$table->timestamps();
});


Here is the problem is, if I have to create the new table, all old scripts runs and show error that table already exists.

is there any way to create table if not exists using Schema builder ?

Answer

Try this

if (!Schema::hasTable('tblCategory')) {
     // create the tblCategory table
}
Comments