Laravel 迁移:使用本机 PHP 枚举
Laravel migration: using a native PHP enum
在 PHP 8.1 中,引入了对枚举的原生支持。我如何在 Laravel 迁移中使用它们?
我的第一个想法是这样的,但它不起作用。
// migration
public function up()
{
Schema::create('school_days', function (Blueprint $table) {
$table->id();
$table->enum('day_of_week', \App\Enums\DayOfWeek::cases());
});
}
// DayOfWeek.php
enum DayOfWeek {
case: Monday;
case: Tuesday;
case: Wednesday;
case: Thursday;
case: Friday;
case: Saturday;
case: Sunday;
}
我不确定 $table->enum
是否实现了枚举,但你可以喜欢这个;
enum DayOfWeek {
case Monday;
case Tuesday;
case Wednesday;
case Thursday;
case Friday;
case Saturday;
case Sunday;
}
$table->enum('day_of_week', array_map(fn($day) => $day->name, DayOfWeek::cases()));
在 PHP 8.1 中,引入了对枚举的原生支持。我如何在 Laravel 迁移中使用它们?
我的第一个想法是这样的,但它不起作用。
// migration
public function up()
{
Schema::create('school_days', function (Blueprint $table) {
$table->id();
$table->enum('day_of_week', \App\Enums\DayOfWeek::cases());
});
}
// DayOfWeek.php
enum DayOfWeek {
case: Monday;
case: Tuesday;
case: Wednesday;
case: Thursday;
case: Friday;
case: Saturday;
case: Sunday;
}
我不确定 $table->enum
是否实现了枚举,但你可以喜欢这个;
enum DayOfWeek {
case Monday;
case Tuesday;
case Wednesday;
case Thursday;
case Friday;
case Saturday;
case Sunday;
}
$table->enum('day_of_week', array_map(fn($day) => $day->name, DayOfWeek::cases()));