Laravel Add new Table with migrate
Asked Answered
L

4

10

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

}
Largeminded answered 5/7, 2015 at 5:42 Comment(3)
Can you please share your migration code?Doggy
Your database already don't have any books table or view, right? Looks everything is fine..Doggy
Yeh, no book table. But some other table are there. So it gives the error..Largeminded
V
4

create new folder in "database/migrations/" and use the "path" property on migrate , Once the table is migrated, you can move the migration file back to the usual folder.

php artisan migrate --path=/database/migrations/new folder/ 
Viceregent answered 20/1, 2019 at 10:2 Comment(0)
P
3

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.

Photooffset answered 5/7, 2015 at 13:34 Comment(0)
J
1

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

Jeanmariejeanna answered 25/7, 2019 at 7:22 Comment(0)
I
0
php artisan migrate:fresh

This command will drop your complete database and reinstall the tables in the migration folder.

Ideal answered 19/8 at 7:37 Comment(1)
Duplicate answerSpitfire

© 2022 - 2024 — McMap. All rights reserved.