Add a new column to existing table in a migration

前端 未结 12 2620
离开以前
离开以前 2020-11-28 17:09

I can\'t figure out how to add a new column to my existing database table using the Laravel framework.

I tried to edit the migration file using...



        
相关标签:
12条回答
  • 2020-11-28 17:59

    First you have to create a migration, you can use the migrate:make command on the laravel artisan CLI.Old laravel version like laravel 4 you may use this command for Laravel 4:

    php artisan migrate:make add_paid_to_users
    

    And for laravel 5 version

    for Laravel 5+:

    php artisan make:migration add_paid_to_users_table --table=users
    

    Then you need to use the Schema::table() . And you have to add the column:

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

    further you can check this

    0 讨论(0)
  • 2020-11-28 17:59

    Laravel 7

    1. Create a migration file using cli command:

      php artisan make:migration add_paid_to_users_table --table=users

    2. A file will be created in the migrations folder, open it in an editor.

    3. Add to the function up():

    Schema::table('users', function (Blueprint $table) {
        // Create new column
        // You probably want to make the new column nullable
        $table->integer('paid')->nullable()->after('status');
    }
    
    1. Add to the function down(), this will run in case migration fails for some reasons:

      $table->dropColumn('paid');

    2. Run migration using cli command:

      php artisan migrate


    In case you want to add a column to the table to create a foreign key constraint:

    In step 3 of the above process, you'll use the following code:

    $table->bigInteger('address_id')->unsigned()->nullable()->after('tel_number');
    
    $table->foreign('address_id')->references('id')->on('addresses')->onDelete('SET NULL');
    

    In step 4 of the above process, you'll use the following code:

    // 1. Drop foreign key constraints
    $table->dropForeign(['address_id']);
    // 2. Drop the column
    $table->dropColumn('address_id');
    
    0 讨论(0)
  • 2020-11-28 18:00

    First rollback your previous migration

    php artisan migrate:rollback
    

    After that, you can modify your existing migration file (add new , rename or delete columns) then Re-Run your migration file

    php artisan migrate
    
    0 讨论(0)
  • 2020-11-28 18:00

    Although a migration file is best practice as others have mentioned, in a pinch you can also add a column with tinker.

    $ php artisan tinker
    

    Here's an example one-liner for the terminal:

    Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){ $table->integer('paid'); })
    



    (Here it is formatted for readability)

    Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){ 
        $table->integer('paid'); 
    });
    
    0 讨论(0)
  • 2020-11-28 18:02

    You can add new columns within the initial Schema::create method like this:

    Schema::create('users', function($table) {
        $table->integer("paied");
        $table->string("title");
        $table->text("description");
        $table->timestamps();
    });
    

    If you have already created a table you can add additional columns to that table by creating a new migration and using the Schema::table method:

    Schema::table('users', function($table) {
        $table->string("title");
        $table->text("description");
        $table->timestamps();
    });
    

    The documentation is fairly thorough about this, and hasn't changed too much from version 3 to version 4.

    0 讨论(0)
  • 2020-11-28 18:04

    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:

    • Schema Builder
    • Migrations

    And for Laravel 4 / Laravel 5:

    • Schema Builder
    • Migrations

    Edit:

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

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