是否可以将旧表中的数据复制到新表中?我们正在计划一个主要的数据库重新安排,将所有数据传递给当前表到新创建的数据库 表
我们意识到处理新闻馈送等数据很容易,这是我的迁移:
/*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();
});
答案 0 :(得分:2)
以下是我对你问题的看法:
1)创建一个迁移文件;
在你的功能中的那个文件中:
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)运行迁移