unsignedBigInteger("gallery")->nullable()->change(); //$table->unsignedBigInteger("tenant")->nullable()->change(); }); Schema::table("traffic", function(Blueprint $table) { $table->unsignedBigInteger("gallery")->nullable()->change(); }); Schema::table("images", function (Blueprint $table) { $table->unsignedBigInteger("tenant"); }); $images = \App\Models\Image::query()->get(); foreach ($images as $image) { if(is_null($image->gallery)) { $image->tenant = 1; $image->saveOrFail(); } else { $gallery = \App\Models\Gallery::query()->where("id", "=", $image->gallery)->firstOrFail(); $image->tenant = $gallery->tenant; $image->saveOrFail(); } } Schema::table("images", function (Blueprint $table) { $table->foreign('tenant')->references('id')->on('tenants'); }); } /** * Reverse the migrations. * * @return void */ public function down() { // } }