Laravel 5.4 Specific Table Migration

前端 未结 21 1383
梦如初夏
梦如初夏 2020-12-07 10:38

Hi read all the included documentation here in https://laravel.com/docs/5.4/migrations.

Is there a way on how to migrate a certain migration file (1 migration only),

相关标签:
21条回答
  • 2020-12-07 11:00

    If you want to create another table, just create a new migration file. It's will work.

    If you create an migration named users_table with id, first_name, last_name. You can create an migration file like

        public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('first_name',255);
                $table->string('last_name',255);
                $table->rememberToken();
                $table->timestamps();
            });
        }
        public function down()
        {
            Schema::dropIfExists('users');
        }
    

    If you want to add another filed like "status" without migrate:refresh. You can create another migration file like "add_status_filed_to_users_table"

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

    And don't forget to add the rollback option:

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

    And when you run migrate with php artitsan migration, It just migrate the new migration file.

    But if you add filed "status" into the first mgration file (users_table) and run migration. It's nothing to migrate. You need to run php artisan migrate:refresh.

    Hope this help.

    0 讨论(0)
  • 2020-12-07 11:01

    php artisan migrate --path=/database/migrations/fileName.php

    Just follow the instruction execute this commant file name here should be your migration table name Example: php artisan migrate --path=/database/migrations/2020_02_21_101937_create_jobs_table.php

    0 讨论(0)
  • 2020-12-07 11:02

    First you should create one migration file for your table like:

    public function up()
        {
            Schema::create('test', function (Blueprint $table) {
                $table->increments('id');
                $table->string('fname',255);
                $table->string('lname',255);
                $table->rememberToken();
                $table->timestamps();
            });
        }
    

    After create test folder in migrations folder then newly created migration moved/copied in test folder and run below command in your terminal/cmd like:

    php artisan migrate --path=/database/migrations/test/
    
    0 讨论(0)
  • 2020-12-07 11:02

    If you want to create one and specific table. You can use this code. It works for laravel(5.x) versions.

    php artisan migrate:refresh --path=/database/migrations/fileName.php
    
    0 讨论(0)
  • 2020-12-07 11:03

    Just look at the migrations table in your database, there will be a list of migration file name and batch number value.

    Suppose you have following structure,

    id     migration                                           batch
    
    1      2014_10_12_000000_create_users_table                  1 
    2      2014_10_12_100000_create_password_resets_table        1
    3      2016_09_07_103432_create_tabel_roles                  1
    

    If you want to just rollback 2016_09_07_103432_create_tabel_roles migration, change it's migration batch value to 2 which is highest among all and then just execute following.

    php artisan migrate:rollback
    

    Here only table with batch value 2 will be rolled back. Now, make changes to that table and run following console command.

    php artisan migrate
    

    Batch value in the migrations table defines order of the migrations. when you rollback, migrations that are latest or have highest batch value are rolled back at first and then others. So, you can change the value in database and then rollback a particular migration file.

    Although it's not a good idea to change batch number every time because of relationship among the table structure, we can use this case for some cases where single table rollback doesn't violates the integrity among the tables.

    Hope you understand.

    0 讨论(0)
  • 2020-12-07 11:06

    Correction- remove slash before the database

    $ php artisan migrate --path=database/migrations/migration.php
    
    0 讨论(0)
提交回复
热议问题