英文:
Laravel 8 model alowed values for column
问题
我需要检查表列的允许值,类似于ENUM。是否可以在模型实体级别设置它?
英文:
I need to chcek alowed values for table column like ENUM. Is it possible to set it up on model entity level?
答案1
得分: 1
Enum:
枚举:
enum ServerStatus: string
{
case Active = 'active';
case Pending = 'pending';
}
Model:
模型:
use App\Enums\ServerStatus;
protected $casts = [
'status' => ServerStatus::class,
]
Validation:
验证:
use App\Enums\ServerStatus;
use Illuminate\Validation\Rules\Enum;
$request->validate([
'status' => [new Enum(ServerStatus::class)],
])
请注意,这是一些代码示例,用于在 Laravel 中创建枚举并将其用于模型和验证。您可以参考提供的链接以获取更多详细信息。
英文:
You need to create an Enum and use it within your Model, and here's how you can do it:
Enum:
enum ServerStatus: string
{
case Active = 'active';
case Pending = 'pending';
}
Model:
use App\Enums\ServerStatus;
protected $casts = [
'status' => ServerStatus::class,
];
Validation:
use App\Enums\ServerStatus;
use Illuminate\Validation\Rules\Enum;
$request->validate([
'status' => [new Enum(ServerStatus::class)],
]);
https://www.php.net/manual/en/language.enumerations.backed.php
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论