Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

php - 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 ?

question from:https://stackoverflow.com/questions/35467605/schema-builder-create-table-if-not-exists

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...