Laravel: Migrations

6

I have a question about Laravel's Migrations and I believe they 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 one more field to the migration: $table->string('nome');

3 - I ran the command php artisan migrate;

My question is, after I ran the php artisan migrate the table was created in the database. And if by chance I need to add another column in the table in the bank, how should I proceed in relation to the respective laravel migration? Should I make direct in the bank or is there a migration command?

    
asked by anonymous 12.09.2015 / 23:43

3 answers

5

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

Run the command php artisan make:migration alter_table_model

This command will create a new migration file in the database/migration folder.

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

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 execute the command php artisan migrate

You will now notice that migration has created a new field in the reported table.

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

Note: When executing the rollback the method called is down . So you should always create this method to revert what was created in the up method.

    
29.07.2016 / 13:40
0

David, what's up?

The migration also supports schema changes, not just creation.

A great library to make this easy is Laravel Generators

link

Command example:

php artisan generate:migration add_user_id_to_posts_table

It will create migration for you by adding the user_id field to posts table. Show né?

This library is an immense facility, it generates all scaffold.

Needing a touch!

Hugs.

    
13.09.2015 / 06:12
0
___ erkimt ___ Laravel: Migrations ______ qstntxt ___

I have a question about Laravel's Migrations and I believe they can help me.

1 - I created a model with migration by %code%

2 - In addition to increments and timestamps I added one more field to the migration: %code%

3 - I ran the command %code%

My question is, after I ran the php artisan migrate the table was created in the database. And if by chance I need to add another column in the table in the bank, how should I proceed in relation to the respective laravel migration? Should I make direct in the bank or is there a migration command?

    
______ azszpr143336 ___

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

Run the command %code%

This command will create a new migration file in the %code% folder.

You should add in the %code% method the following line of code:

%pre%

In the down method:

%pre%

Then just execute the command %code%

You will now notice that migration has created a new field in the reported table.

If you want to create a new field without having to create a new migration, you can run %code% to return the database table to the previous state, edit the migration file, and re-execute %code% for new changes.

Note: When executing the rollback the method called is %code% . So you should always create this method to revert what was created in the %code% method.

    
______ azszpr86674 ___

David, what's up?

The migration also supports schema changes, not just creation.

A great library to make this easy is Laravel Generators

link

Command example:

%code%

It will create migration for you by adding the %code% field to %code% table. Show né?

This library is an immense facility, it generates all scaffold.

Needing a touch!

Hugs.

    
______ ___ azszpr194544

only use the php artisan migrate:. Refresh after you change your migrate file

It will make a rollback of your tables, and will recreate them.

    
___
02.04.2017 / 19:53