繁体   English   中英

数据从旧表迁移到新表,使用 Laravel 5

[英]Data migration transferring from old table into new table, with Laravel 5

是否可以将旧表中的数据复制到新表中? 我们正在计划一个主要的数据库重新安排,将所有数据传递到当前表到一个新创建的表。

我们意识到处理新闻源等数据会很容易。这是我的迁移:

/*students table*/

    public function up()
        {
            Schema::create('students', function (Blueprint $table) {
                $table->increments('id');
                $table->string('lname');
                $table->string('mname')->nullable();
                $table->string('fname');
                $table->char('gender', 1);
                $table->date('date_of_birth');
                $table->string('address');
                $table->tinyInteger('yr_lvl');
                $table->string('contact_no');
                $table->text('about_me')->nullable();
                $table->text('education')->nullable();
                $table->text('achievements')->nullable();
                $table->text('seminars')->nullable();
                $table->text('organizations')->nullable();
                $table->tinyInteger('status')->default(1);
                $table->timestamps();
            });
        }

    /*newly created table works*/
    Schema::create('works', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('student_id')->unsigned()->nullable();
                $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
                $table->text('about_me')->nullable();
                $table->timestamps();
            });

这是我对你的问题的想法:

1) 创建迁移文件;

在你 up 函数中的那个文件中:

2)从旧表中创建一个对象,并选择所需的字段

现在

3) 写入新表的迁移代码。

现在开始一个 foreach 循环并存储数据:

$oldData  = OLDTABLE::select('your_fields')->get();

Schema::create('works', function (Blueprint $table) {
                    $table->increments('id');
                    $table->integer('student_id')->unsigned()->nullable();
                    $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
                    $table->text('about_me')->nullable();
                    $table->timestamps();
                });

foreach ($oldData as $data){
  $newData = new Work();
  $newData->student_id = $data->student_id
  $newData->save();
}

4)运行你的迁移

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM