laravellaravel-migrations

Add sql table column before or after specific other column - by migrations in Laravel 4.1


Table 'users':

|id|name|address|post_code|deleted_at|created_at|

and I want add column 'phone_nr' somewhere between 'id' and 'deleted_at'

Is it possible by migrations in Laravel 4.1?


Solution

  • Yes. Create a new migration using php artisan make:migration update_users_table.

    Then use the table command as follows (if you're using MySQL!):

    Schema::table('users', function($table)
    {
        $table->string('phone_nr')->after('id');
    });
    

    Once you've finished your migration, save it and run it using php artisan migrate and your table will be updated.

    Documentation: https://laravel.com/docs/12.x/migrations#column-modifiers