refactor(website): update relationships and migration to use 'website_type_code' instead of 'website_type'

This commit is contained in:
2026-07-29 15:03:31 -03:00
parent b9ad2e589f
commit 9e3163b74f
4 changed files with 9 additions and 7 deletions

View File

@@ -22,7 +22,7 @@ class WebsiteType extends Model
*/ */
public function extras(): HasMany public function extras(): HasMany
{ {
return $this->hasMany(WebsiteTypeExtra::class, 'website_type'); return $this->hasMany(WebsiteTypeExtra::class, 'website_type_code', 'codigo');
} }
/** /**

View File

@@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
#[Fillable([ #[Fillable([
'website_type', 'website_type_code',
'nombre', 'nombre',
'descripcion', 'descripcion',
'is_required', 'is_required',
@@ -27,7 +27,6 @@ class WebsiteTypeExtra extends Model
protected function casts(): array protected function casts(): array
{ {
return [ return [
'website_type' => 'integer',
'is_required' => 'boolean', 'is_required' => 'boolean',
'config_schema' => 'array', 'config_schema' => 'array',
]; ];
@@ -38,7 +37,7 @@ class WebsiteTypeExtra extends Model
*/ */
public function websiteType(): BelongsTo public function websiteType(): BelongsTo
{ {
return $this->belongsTo(WebsiteType::class, 'website_type'); return $this->belongsTo(WebsiteType::class, 'website_type_code', 'codigo');
} }
/** /**

View File

@@ -27,8 +27,10 @@ return new class extends Migration
Schema::create('website_type_extras', function (Blueprint $table): void { Schema::create('website_type_extras', function (Blueprint $table): void {
$table->id(); $table->id();
$table->foreignId('website_type') $table->string('website_type_code');
->constrained('website_type') $table->foreign('website_type_code')
->references('codigo')
->on('website_type')
->cascadeOnUpdate() ->cascadeOnUpdate()
->cascadeOnDelete(); ->cascadeOnDelete();
$table->string('nombre'); $table->string('nombre');

View File

@@ -26,7 +26,7 @@ class WebsiteExtrasTest extends TestCase
$this->assertEqualsCanonicalizing([ $this->assertEqualsCanonicalizing([
'id', 'id',
'website_type', 'website_type_code',
'nombre', 'nombre',
'descripcion', 'descripcion',
'is_required', 'is_required',
@@ -68,6 +68,7 @@ class WebsiteExtrasTest extends TestCase
]); ]);
$this->assertTrue($type->extras()->firstOrFail()->is($typeExtra)); $this->assertTrue($type->extras()->firstOrFail()->is($typeExtra));
$this->assertSame($type->codigo, $typeExtra->website_type_code);
$this->assertTrue($type->tenants()->firstOrFail()->is($tenant)); $this->assertTrue($type->tenants()->firstOrFail()->is($tenant));
$this->assertTrue($tenant->websiteType()->firstOrFail()->is($type)); $this->assertTrue($tenant->websiteType()->firstOrFail()->is($type));
$this->assertSame($type->codigo, $tenant->website_type_code); $this->assertSame($type->codigo, $tenant->website_type_code);