ENUM types are either unavailable (e.g., PostgreSQL supports them, but MySQL does not consistently) or where developers prefer business-logic-driven enums over database-level constraints. It maps well to domain-driven design (DDD) patterns, where enums represent bounded contexts (e.g., OrderStatus, UserRole).$user->role === 'ADMIN') with typed enums ($user->role->isAdmin()).fromDatabase()/toDatabase() methods centralize conversion logic, reducing boilerplate compared to manual implementations.$user->role = new UserRole('ADMIN')).FormRequest or Validator to enforce allowed enum values.{"status": "PENDING"}).ENUM types in migrations, dropping them to use strings may require schema changes. Mitigate by using a migration to alter columns to VARCHAR first.if ($status == 'ACTIVE')) with enum methods (e.g., $status->isActive()). Use IDE refactoring tools or static analysis (e.g., PHPStan).User::all()) may increase memory usage. Profile with tools like Blackfire.strict_types=1 and IDE autocompletion to reduce risks.enum + custom toString()/from() methods?Rule::in() or custom validation rules?class User extends Model {
protected $casts = [
'role' => EnumMapper::class, // Hypothetical; package may need custom casting.
];
}
public function rules() {
return [
'status' => ['required', Rule::in(array_column(OrderStatus::cases(), 'value'))],
];
}
public function toArray($request) {
return [
'status' => $this->status->toHuman(),
];
}
GET /api/statuses returns ["PENDING", "APPROVED"]).if ($user->status == 'active')).EnumMapper class (or extend the package) for a single enum (e.g., OrderStatus).public function getStatusAttribute($value) {
return OrderStatus::fromDatabase($value);
}
public function setStatusAttribute($value) {
$this->attributes['status'] = OrderStatus::toDatabase($value);
}
// Before
if (!in_array($request->status, ['pending', 'approved'])) { ... }
// After
if (!$this->status->isValid()) { ... }
VARCHAR with sufficient length (e.g., ENUM in MySQL maxes at 64 chars).DB::table('orders')->update([
'status' => DB::raw("CASE status
WHEN 'pending' THEN 'PENDING'
WHEN 'approved' THEN 'APPROVED'
ELSE status END"),
]);
casts property may require custom logic (package may not support it out-of-the-box).ENUM-like behavior (e.g., PostgreSQL’s text vs. MySQL’s ENUM).spatie/laravel-permission.User::preferences) before core logic (e.g., Order::status).Payment::status) until the pattern is proven stable.config or environment variables to toggle enum behavior during migration.How can I help you explore Laravel packages today?