Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 838
user3419778 Avatar asked Jul 05 '15 05:07

user3419778


People also ask

How do I add a column to a table in Laravel migration?

The Laravel migrations will use the Schema facade to create and modify database tables and columns: Schema::create('tasks', function (Blueprint $table) { $table->bigIncrements('id'); $table->timestamps(); }); Inside the facade, you could specify the different columns that you want to add.

How do I move a table in Laravel without losing data?

Set your column details there, run the migrations using php artisan migrate and that's all. You'll have this new column in your users table without losing previously stored data.

What is migration table in Laravel?

Migrations are like version control for your database, allowing your team to modify and share the application's database schema. Migrations are typically paired with Laravel's schema builder to build your application's database schema.


2 Answers

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/ 
like image 103
mjmiri Avatar answered Oct 26 '22 00:10

mjmiri


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.

like image 45
cre8 Avatar answered Oct 26 '22 00:10

cre8