id(); $table->timestamp('created_at',0)->nullable(); $table->string('version'); $table->index(['version']); }); foreach (DB::table('site_versions')->select(['version',DB::raw('MIN(created_at) AS created_at')])->groupBy('version')->cursor() as $o) { $oo = new \App\Models\Version; $oo->created_at = $o->created_at; $oo->version = $o->version; $oo->save(); } Schema::table('site_versions', function (Blueprint $table) { $table->bigInteger('version_id')->nullable(); $table->foreign('version_id')->references('id')->on('versions'); $table->dropColumn(['updated_at']); }); foreach (DB::table('versions')->select(['id','version'])->cursor() as $o) { DB::table('site_versions')->where('version',$o->version)->update(['version_id'=>$o->id]); } Schema::table('site_versions', function (Blueprint $table) { $table->dropColumn(['version']); }); Schema::table('sites', function (Blueprint $table) { $table->dropColumn(['updated_at']); }); } /** * Reverse the migrations. */ public function down(): void { throw new \Exception('Cant go back'); } };