有没有办法在Laravel 进行data次迁徙?

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).

推荐答案

Laravel has migrations built in :) 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.

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');
        });
    }
}

Laravel相关问答推荐

@vite指令在使用laravel vite构建后导致错误

LaravelEloquent 的模型如何将值从控制器内部的函数传递到Render()

Laravel FAKER语法

如何使用 laravel 更新单个值

LARVAL SAVE 中的关系有

如何使用动态翻译键翻译 Laravel硬编码字符串(还有 1 个错误)..(验证异常类)

不能在 laravel 中使用控制器名称,必须使用命名空间

Laravel 5 - ErrorException 无法打开流:权限被拒绝

Laravel 验证 - 输入必须是数组中的项目之一

你如何在自定义 Laravel Nova 工具中使用确认对话?

如何在 Laravel Eloquent 中 Select 某些字段?

laravel 搜索多个以空格分隔的单词

在共享主机中仅使用 FTP 部署 Laravel 5

未找到列:1054 未知列 laravel

Laravel - 语法错误,文件意外结束

Laravel:方法[显示]不存在

Laravel 5 - 从控制器级别的所有请求对象中删除参数

带有 Xdebug 的 Laravel 5 总是抛出The payload is invalid..

count() 参数必须是数组或在 laravel 中实现可数的对象

phpunit 命令不适用于 Windows 7 上的 laravel 4