Add a new column to existing table in a migration

前端 未结 12 2619
离开以前
离开以前 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:44

    WARNING this is a destructive action. If you use this ensure you back up your database first

    you can simply modify your existing migration file, for example adding a column in your table, and then in your terminal typing :

    $ php artisan migrate:refresh
    
    0 讨论(0)
  • 2020-11-28 17:47

    If you're using Laravel 5, the command would be;

    php artisan make:migration add_paid_to_users
    

    All of the commands for making things (controllers, models, migrations etc) have been moved under the make: command.

    php artisan migrate is still the same though.

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

    Add column to your migration file and run this command.

    php artisan migrate:refresh --path=/database/migrations/your_file_name.php
    
    0 讨论(0)
  • 2020-11-28 17:56

    I'll add on to mike3875's answer for future readers using Laravel 5.1 and onward.

    To make things quicker, you can use the flag "--table" like this:

    php artisan make:migration add_paid_to_users --table="users"
    

    This will add the up and down method content automatically:

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            //
        });
    }
    

    Similarily, you can use the --create["table_name"] option when creating new migrations which will add more boilerplate to your migrations. Small point, but helpful when doing loads of them!

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

    laravel 5.6 and above

    in case you want to add new column as a FOREIGN KEY to an existing table.

    Create a new migration by executing this command : make:migration

    Example :

    php artisan make:migration add_store_id_to_users_table --table=users
    

    In database/migrations folder you have new migration file, something like :

    2018_08_08_093431_add_store_id_to_users_table.php (see the comments)

    <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class AddStoreIdToUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
    
                // 1. Create new column
                // You probably want to make the new column nullable
                $table->integer('store_id')->unsigned()->nullable()->after('password');
    
                // 2. Create foreign key constraints
                $table->foreign('store_id')->references('id')->on('stores')->onDelete('SET NULL');
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
    
                // 1. Drop foreign key constraints
                $table->dropForeign(['store_id']);
    
                // 2. Drop the column
                $table->dropColumn('store_id');
            });
        }
    }
    

    After that run the command :

    php artisan migrate
    

    In case you want to undo the last migration for any reason, run this command :

    php artisan migrate:rollback
    

    You can find more information about migrations in the docs

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

    this things is worked on laravel 5.1.

    first, on your terminal execute this code

    php artisan make:migration add_paid_to_users --table=users
    

    after that go to your project directory and expand directory database - migration and edit file add_paid_to_users.php, add this code

    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
             $table->string('paid'); //just add this line
        });
    }
    

    after that go back to your terminal and execute this command

    php artisan migrate
    

    hope this help.

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