简体   繁体   中英

Add extra table using Laravel Migration

I am using Laravel 5.2. I have created basic tables using migration method. But now I need to add another extra table. After creating my schema, I have given php artisan migrate command. But it shows error like base table or view already exists Table:Users . I know why this happen. The migration command trying to recreate the table which already have. But I need to add another extra table in Laravel via Migration. I have gone through this https://laravel.com/docs/5.2/migrations But I can't get any solution.

If you want to create another table, just create new migration and run it.

If you're trying to add columns into existinng table, use Schema::table instad of Schema::create .

Schema::create('articles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
}

Schema::table('articles', function (Blueprint $table) {
    $table->string('description');
}

If you already executed php artisan migrate then next time it will give you error saying "Table already exists.".

So if you want to execute only a particular migration then either you can temporarily move all migration's php file which are executed, out of database/migrations folder and then execute

php artisan migrate

or

you can execute migration from tinker ie first execute php artisan tinker and then execute content of up method from the migration but without parameter type Blueprint .

Eg. If following is your migration up method content

Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
});

then you will have to execute

Schema::create('users', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM