Laravel 数据迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23506286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:29:18  来源:igfitidea点击:

Laravel data migrations

phplaravel

提问by Eduardo Matos

Is there a way to make datamigrations in Laravel? I've found some instructions on how to seed the database, but it doesn't cover cases where I need to split one field into multiple fields, or merge multiple fields into one.

有没有办法在 Laravel 中进行数据迁移?我找到了一些关于如何为数据库设置种子的说明,但它不包括我需要将一个字段拆分为多个字段或将多个字段合并为一个的情况。

One possible solution is to query the database and update each record on a loop. The problem with this approach is that the models may not reflect the table schema during the migration (Django provides a solution for this).

一种可能的解决方案是查询数据库并在循环中更新每条记录。这种方法的问题是模型在迁移过程中可能无法反映表模式(Django 为此提供了解决方案)。

回答by Kisuka

Laravel has migrations built in :) http://laravel.com/docs/migrations

Laravel 内置了迁移:) http://laravel.com/docs/migrations

Simply run

只需运行

php artisan make:migration migration_name_here

and it will create a migration under app/database/migrations. You could then use Laravel's database classes in your up() and down() methods.

它将在 app/database/migrations 下创建一个迁移。然后你可以在 up() 和 down() 方法中使用 Laravel 的数据库类。

Let's use this as an example...

让我们以此为例...

class SplitColumn extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('table_name', function($table)
        {
            // Create new columns for table_name (1 column split into 2).
            $table->string('new_column');
            $table->string('new_column_b');
        });

        // Get records from old column.
        $results = DB::table('table_name')->select('old_column')->get();

        // Loop through the results of the old column, split the values.
        // For example, let's say you have to explode a |.
        foreach($results as $result)
        {
            $split_value = explode("|", $result->old_column);

            // Insert the split values into new columns.
            DB::table('table_name')->insert([
                "new_column"    =>  $split_value[0],
                "new_column_b"  =>  $split_value[1]
            ]);
        }

        // Delete old column.
        Schema::table('table_name', function($table)
        {
            $table->dropColumn('old_column');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('table_name', function($table)
        {
            // Re-create the old column.
            $table->string('old_column');
        });

        // Get records from old column.
        $results = DB::table('table_name')->select('new_column', 'new_column_b')->get();

        // Loop through the results of the new columns and merge them.
        foreach($results as $result)
        {
            $merged_value = implode("|", [$result->new_column, $result->new_column_b]);

            // Insert the split values into re-made old column.
            DB::table('table_name')->insert([
                "old_column"    =>  $merged_value
            ]);
        }

        // Delete new columns.
        Schema::table('table_name', function($table)
        {
            $table->dropColumn('new_column');
            $table->dropColumn('new_column_b');
        });
    }
}