Schema Builder : Create Table if not exists
Please check the answer here
To create a new table there is only one check by Laravel Schema function hasTable
.
But if you want to drop any table before checking its existence then Schema have a function called dropIfExists
.
Schema::dropIfExists('table_name');
It will drop the table if table will exist.
Try this
if (!Schema::hasTable('tblCategory')) {
Schema::create('tblCategory', function($table){
$table->engine = 'InnoDB';
$table->increments('CategoryID');
$table->string('Category', 40);
$table->unique('Category', 'tblCategory_UK_Category');
$table->timestamps();
}
}
DB::transaction(function () {
// Create your table here with schema builder.
});
If an exception is thrown within the transaction Closure, the transaction will automatically be rolled back. So trying to create a table already existing will throw an error. Most database transactions should have this closure in Laravel.
https://laravel.com/docs/5.6/database