Recents in Beach

header ads

How to modify/alter laravel migrations

https://stackoverflow.com/a/16791988/9127475

 To create a migration, you may use the migrate:make command on the Artisan CLI. Use a specific name to avoid clashing with existing models

for Laravel 3:

php artisan migrate:make add_paid_to_users

for Laravel 5+:

php artisan make:migration add_paid_to_users_table --table=users

You then need to use the Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this:

public function up()
{
    Schema::table('users', function($table) {
        $table->integer('paid');
    });
}

and don't forget to add the rollback option:

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('paid');
    });
}

Then you can run your migrations:

php artisan migrate

This is all well covered in the documentation for both Laravel 3:

And for Laravel 4 / Laravel 5:

Edit:

use $table->integer('paid')->after('whichever_column'); to add this field after specific column.


https://laracasts.com/discuss/channels/laravel/update-table-from-migrate?page=1&replyId=368145


create a new migration... make:migration add_avatar_to_users

then

 Schema::table('users', function (Blueprint $table) {
            $table->string('avatar')->nullable()->after('password');
        });

Note: don't forget your "down" too for rollbacks...

 Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('avatar');
        });

EDIT: added the ->after() part just to match your OP aims



https://stackoverflow.com/a/33900692/9127475

To do this you should follow these steps:

  1. create a new migration file

    php artisan make:migration update_votes_table
    
  2. open the newly created migration file (app_folder\database\migrations{date_migrationfile_was_created}-update_votes_tables.php)

  3. change the columns you want to change

For more details see the documentation on database migrations

Note: If you add your migrations file to the question we could provide more detailed help



https://laracasts.com/discuss/channels/laravel/can-you-rename-migration-files-to-your-own-custom-format?page=1&replyId=485693


Yes, you can name them however you want, but they will run in alphabetical order (which is why laravel timestamps them). Also you can change a migrations name after the fact (you've already run the migration). You just need to change the filename in the migrations database table (so it can find it to roll it back) in addition to changing the actual filename and class name.

I'd use a 3 digit number for the prefix. (001_migrationname) to leave enough room for future migrations. You never know.

Post a Comment

0 Comments