Refactor Prop model to use enum for data types; remove PropDataType model and update related requests and migrations
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
|
||||
namespace App\Domains\Prop\Models;
|
||||
|
||||
use App\Domains\Prop\Support\PropDataType;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable([
|
||||
@@ -14,12 +14,23 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
'codigo',
|
||||
'nombre',
|
||||
'is_required',
|
||||
'data_type_id',
|
||||
'data_type',
|
||||
])]
|
||||
class Prop extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'is_required' => 'boolean',
|
||||
'data_type' => PropDataType::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<self> $query
|
||||
*/
|
||||
@@ -28,14 +39,6 @@ class Prop extends Model
|
||||
$query->where('propable_type', is_string($model) ? $model : $model::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<PropDataType, $this>
|
||||
*/
|
||||
public function dataType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(PropDataType::class, 'data_type_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<PropOption, $this>
|
||||
*/
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Prop\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable([
|
||||
'name',
|
||||
'code',
|
||||
])]
|
||||
class PropDataType extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return HasMany<Prop, $this>
|
||||
*/
|
||||
public function props(): HasMany
|
||||
{
|
||||
return $this->hasMany(Prop::class, 'data_type_id');
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Domains\Prop\Requests;
|
||||
|
||||
use App\Domains\Prop\Support\PropDataType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
@@ -21,7 +22,7 @@ class StorePropRequest extends FormRequest
|
||||
'codigo' => ['required', 'string', 'max:255', Rule::unique('props', 'codigo')],
|
||||
'nombre' => ['required', 'string', 'max:255'],
|
||||
'is_required' => ['sometimes', 'boolean'],
|
||||
'data_type_id' => ['required', 'integer', 'exists:props_data_types,id'],
|
||||
'data_type' => ['required', Rule::enum(PropDataType::class)],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Domains\Prop\Requests;
|
||||
|
||||
use App\Domains\Prop\Models\Prop;
|
||||
use App\Domains\Prop\Support\PropDataType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
@@ -25,7 +26,7 @@ class UpdatePropRequest extends FormRequest
|
||||
'codigo' => ['required', 'string', 'max:255', Rule::unique('props', 'codigo')->ignore($prop?->id)],
|
||||
'nombre' => ['required', 'string', 'max:255'],
|
||||
'is_required' => ['sometimes', 'boolean'],
|
||||
'data_type_id' => ['required', 'integer', 'exists:props_data_types,id'],
|
||||
'data_type' => ['required', Rule::enum(PropDataType::class)],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
namespace App\Domains\Prop\Resources;
|
||||
|
||||
use App\Domains\Prop\Models\Prop;
|
||||
use App\Domains\Prop\Support\PropableModels;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @mixin \App\Domains\Prop\Models\Prop
|
||||
* @mixin Prop
|
||||
*/
|
||||
class PropResource extends JsonResource
|
||||
{
|
||||
@@ -17,11 +19,11 @@ class PropResource extends JsonResource
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'propable_type' => $this->propable_type,
|
||||
'propable_type' => PropableModels::aliasFor($this->propable_type) ?? $this->propable_type,
|
||||
'codigo' => $this->codigo,
|
||||
'nombre' => $this->nombre,
|
||||
'is_required' => $this->is_required,
|
||||
'data_type_id' => $this->data_type_id,
|
||||
'data_type' => $this->data_type?->value,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
];
|
||||
|
||||
26
app/Domains/Prop/Support/PropDataType.php
Normal file
26
app/Domains/Prop/Support/PropDataType.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Prop\Support;
|
||||
|
||||
enum PropDataType: string
|
||||
{
|
||||
case String = 'string';
|
||||
case Integer = 'integer';
|
||||
case Decimal = 'decimal';
|
||||
case Boolean = 'boolean';
|
||||
case Date = 'date';
|
||||
case DateTime = 'datetime';
|
||||
case Json = 'json';
|
||||
case Select = 'select';
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function values(): array
|
||||
{
|
||||
return array_map(
|
||||
static fn (self $type): string => $type->value,
|
||||
self::cases(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -39,4 +39,9 @@ class PropableModels
|
||||
|
||||
return $modelClass;
|
||||
}
|
||||
|
||||
public static function aliasFor(string $modelClass): ?string
|
||||
{
|
||||
return array_search($modelClass, static::map(), true) ?: null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user