Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 680
Pankaj Avatar asked Feb 17 '16 20:02

Pankaj


People also ask

How do you check table is exists or not in our database using laravel?

Change your 'table_name' value. If you get one row output, it means the table exists.

How do I know if a table is empty in laravel?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

What does nullable mean in laravel?

It means the middle_name field can also store null values: as in inserting a value is not required.

How do I rollback laravel migration?

How to rollback a specific migration in laravel ? Step 1 : Open database and check migrations table. Step 2 : Change batch number to last number. Step 3 : Run artisan rollback command.


1 Answers

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();     } } 
like image 50
paranoid Avatar answered Sep 21 '22 18:09

paranoid