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

前端 未结 5 1454
遇见更好的自我
遇见更好的自我 2020-12-14 05:56

Table \'users\':

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

and I want add column \'phone_nr\' somewhere between \'id\' and \'delete

相关标签:
5条回答
  • 2020-12-14 06:04

    The answer by James is still correct. However, Laravel 5 has slightly changed the make migration syntax:

    php artisan make:migration add_google_auth_to_users_table --table=users
    

    (I am aware this question is tagged Laravel 4, however it ranks quite high for the question ;) )

    0 讨论(0)
  • 2020-12-14 06:09

    For anyone else wondering about @rahulsingh's query about adding multiple columns back to back after a single specific column:

    $table->integer('col1')->after('ref_col');
    $table->integer('col2')->after('what');
    

    You can do this just by writing the new fields in reverse order, for example:

    $table->integer('col4')->after('ref_col');
    $table->integer('col3')->after('ref_col');
    $table->integer('col2')->after('ref_col');
    $table->integer('col1')->after('ref_col');
    

    When you run this migration you'll find that your new fields col, col2, ... are in your intended order after the reference field ref_col.

    Unfortunately there is no before() function for adding fields before an existing field. (If that existed we could keep the above statements in their real order.)


    You cannot do chaining with fields that do not yet exist, for example:

    $table->integer('col1')->after('ref_col');
    $table->integer('col2')->after('col1');
    $table->integer('col3')->after('col2');
    $table->integer('col4')->after('col3');
    

    This is because the migration blueprint is compiled to a single query thus executed as one, so when it comes to adding col2 your database engine will complain that col1 doesn't exist.

    0 讨论(0)
  • 2020-12-14 06:17

    Yes. Create a new migration using php artisan migrate:make 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/4.2/migrations#column-modifiers

    0 讨论(0)
  • 2020-12-14 06:29

    I've created following migration:

    php artisan make:migration update_nvm_table

    2019_07_10_130441_update_nvm_table.php
    

    And inside:

    public function up()
        {
            Schema::table('nvm', function (Blueprint $table) {
                $table->renameColumn('data1', 'call_owner');
                $table->renameColumn('data2', 'transfer_type');
                $table->string('transfer_data', 50)->nullable();
            });
        }
    
     public function down()
        {
            Schema::table('nvm', function (Blueprint $table) {
                $table->dropColumn('transfer_data');
                $table->renameColumn('call_owner', 'data1');
                $table->renameColumn('transfer_type', 'data2');
            });
        }
    

    I hope it can a reference for you! :D

    0 讨论(0)
  • 2020-12-14 06:29

    For latest versions of Laravel say 5-6

    The command is

    php artisan make:migration update_users_table
    

    Then

    php artisan migrate
    
    0 讨论(0)
提交回复
热议问题