Laravel: Migrations

Asked

Viewed 5,260 times

6

I have a question about Migrations do Laravel and I believe you can help me.

1 - I created a model with Migration by php artisan make:model Evento -m;

2 - In addition to increments and timestamps I added another field to Migration: $table->string('nome');

3 - I have directed the command php artisan migrate;

My question is, after I went to php Artisan migrate the table was created in the database. And if I need to add one more column to the table in the bank, how should I proceed with the respective Laravel Migration? I must do it directly at the bank or there is a command by Migration?

3 answers

5

After you have created a Migration and still want to add a new field, you can proceed as follows:

Execute the command php artisan make:migration alter_table_model

With this command a new Migration file will be created in the folder database/migration.

You should add in the method up the following code line:

Schema::table('nome da tabela',function($t){
   $t->string('novo_campo')->nullable();
});

In the down method:

Schema::table('nome da tabela',function($t){
       $t->dropColumn('novo_campo');
    });

Then just run the command php artisan migrate

Now you will notice that Migration has created a new field in the informed table.

If you want to create a new field without having to create a new Migration, you can execute php artisan migrate:rollback to return the database table to the previous state, edit the Migration file and run again php artisan migrate for new changes.

Observing: When performing the rollback the called method is the down. So you should always create this method in order to reverse what was created in the method up.

  • Miguel, every time I run a "php Artisan migrate", even trying another "alter_table_model" migrate, the command recreates the entire database again. This is a problem if I already have data in the database, no?

0

Speak David, all good?

Migration also supports schema changes, not just creation.

A great library to facilitate this is Laravel Generators

https://github.com/JeffreyWay/Laravel-4-Generators

Example of the command:

php artisan generate:migration add_user_id_to_posts_table

It will create Migration for you by adding the field user_idfor table posts. Show right?

This library is an immense facility, it generates all the scaffolding.

In need of a touch!

Hugs.

  • 1

    All right, Junior. I think this Migration thing is really tricky. Up and Down, migrate and migrate:rollback. Maybe it’s just a case of finding a good tutorial. But for now this is the only question that for me, regarding Laravel, is not yet well clarified. Do you know any detailed tutorial about Migrations? Hug.

  • @Davidcoelho https://laracasts.com/search?q=migration&q-where=lessons recommend.

  • This library is for Laravel 4

  • @Wallacemaxters yes. But natively has generation of Migrations. Only run php artisan make:migration nome

0

Just use the command php Artisan migrate:refresh after changing your migrate file.

He will make a rollback of its tables, and will recreate them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.