id(); }); Schema::create('users', function (Blueprint $table): void { $table->id(); $table->string('tenant_codigo')->nullable(); $table->softDeletes(); $table->timestamps(); }); DB::table('users')->insert(['id' => 1, 'tenant_codigo' => 'legacy']); DB::table('events')->insert(['id' => 42]); $migration = require database_path('migrations/2026_10_01_000000_add_event_id_to_users_table.php'); $migration->up(); $legacy = User::query()->findOrFail(1); $this->assertNull($legacy->event_id); $this->assertNull($legacy->event); $this->assertSame('legacy', $legacy->tenant_codigo); $legacy->update(['event_id' => '42']); $user = $legacy->fresh(); $this->assertSame(42, $user->event_id); $this->assertSame(42, $user->event->id); $this->assertSame(42, UserResource::make($user)->resolve(new Request)['event_id']); $this->assertSame(42, AdministratorResource::make($user)->resolve(new Request)['event_id']); try { DB::table('events')->where('id', 42)->delete(); $this->fail('An assigned event must not be deleted.'); } catch (QueryException $exception) { $this->assertStringContainsString('FOREIGN KEY', $exception->getMessage()); } $migration->down(); $this->assertFalse(Schema::hasColumn('users', 'event_id')); $this->assertSame('legacy', DB::table('users')->where('id', 1)->value('tenant_codigo')); } }