Helper Helper - 1 year ago 128
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->string('Category', 40);
$table->unique('Category', 'tblCategory_UK_Category');

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 Source

Try this

if (!Schema::hasTable('tblCategory')) {
     // create the tblCategory table
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download