2016-04-04 12 views
1

古いテーブルのデータを新しいテーブルにコピーすることはできますか?私たちは、現在のテーブルにすべてのデータを新しく作成した テーブルに渡す大規模なデータベースの再配置を計画しています。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(); 
      }); 
+0

を移行実行'テーブル(別の構造を持つ)? –

+0

いいえ私はちょうど新しいテーブルにいくつかのデータ(about_me)をコピーしたいと思います...私は他のテーブル(作品)を見ることができます私のフィールドと学生からの外国キー –

答えて

0

はあなたの問題のために私の考えです。)

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)あなたは、異なるデータ構造を持つ「古い」テーブルを持っていて、「新しいにそのデータを転送する必要が

+0

ありがとう..ありがとう –

関連する問題