简体   繁体   中英

Laravel Add new Table with migrate

I have created a Laravel project and I added few table with Migrate. It was working fine. The tables already had data. Now I require another table and I tried to add it with this command:

php artisan make:migration create_books_table

It is working and add files at \\database\\migrations.... I then added schema at up() function. But when I run the command,

php artisan migrate

It is not working , giving error Base table or view already exists.

Please help. I am very new to laravel. Thanks

Migration code..

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBooksTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->text('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('books');
    }

}

Laravel has an own table called migration. It's used to save information about when a table was created so artisan can rollback to another point. If you delete a table manually the entry in the migration table is still there. If you remove all tables in your database and migrate again it should work.

在“database / migrations /”中创建新文件夹,并在迁移时使用“path”属性。迁移表后,可以将迁移文件移回常用文件夹。

php artisan migrate --path=/database/migrations/new folder/ 

You need to delete the existing 'books' table in the database;

The table already exists so it wont get created again.

Or if you donot have too much data in the other tables run

php artisan migrate:fresh

This will roll back previous table migrations and re migrate them all

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