mpe*_*pen 50 php enums laravel
Laravel有一个<select>表单助手,它将字典作为输入.我喜欢把所有这些的价值保持在一个中心位置.例如,我可能有一个看起来像这样的枚举:
$phoneTypes = [
    'CELL' => "Cellular",
    'HOME' => "Home",
    'WORK' => "Work",
];
我想在我的视图/模板和数据库中使用它:
Schema::create('customers', function (Blueprint $table) {
    $table->increments('id');
    $table->enum('pri_phone_type',array_keys($phoneTypes));
    ...
});
Ban*_*ord 58
我不同意这里接受的答案.我觉得枚举对于这种事情非常有用.我更喜欢将枚举视为类型,并在Enum基类上实现所需的方法,以便为您提供所需的功能,例如获取字典.
我的简单示例如下:
abstract class PhoneType extends Enum {
    const Cell = "Cellular";
    const Home = "Home";
    const Work = "Work";
}
abstract class Enum {
    static function getKeys(){
        $class = new ReflectionClass(get_called_class());
        return array_keys($class->getConstants());
    }
}
用法示例:
PhoneType::getKeys();
有关更多详细信息和更深入的示例,请参阅PHP和枚举.
jsz*_*ody 56
您有几种处理枚举的选项.在我们看一些之前,我首先强烈建议您不要使用DB enum列类型.
出于多种原因,数据库枚举存在问题.我建议阅读这篇文章,例如:
http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/
因此,让我们看看其他几个选项.
由于您使用的是Laravel,因此一个非常简单的选择是在配置文件中添加一组选项.
假设您config/enums.php使用以下内容创建新文件:
return [
    'phone_types' => [
        'CELL' => "Cellular",
        'HOME' => "Home",
        'WORK' => "Work",
    ]
];
您现在可以访问config('enums.phone_types')代码中的任何位置,包括您的Blade模板.
@Banford的答案显示了如何使用类常量执行基本的枚举类型行为.如果您喜欢这种方法,我建议您查看本文和基于此概念构建的包,以提供强类型枚举:
https://stitcher.io/blog/php-enums
https://github.com/spatie/enum
你会创建一个这样的类:
/**
 * @method static self cell()
 * @method static self home()
 * @method static self work()
 */
class PhoneTypes extends Enum
{
}
现在你可以PhoneTypes::home()在你的应用程序中调用.查看该软件包的文档,了解如何创建值映射(如果需要).
如果您真的想要管理数据库中的选项,我将创建一个单独的phone_types数据库表并与您的customers表创建关系.这仍然是比使用enum列类型更好的选择.
Kir*_*chs 13
基于@Banfords答案,PHP7常量现在可以是数组:
class User extends Authenticatable
{
    /**
     * The possible genders a user can be.
     */
    const GENDER = [
        'Male',
        'Female',
        'Unspecified'
    ];
...
小智 7
除了@ Banford的回答:
我最近整理了一个包,使得在Laravel中使用枚举更好.这是我在研究如何做同样事情时发现的各种实现的组合(因此我在这里).
https://github.com/BenSampo/laravel-enum
在这种情况下,您可以执行以下操作:
final class PhoneTypes extends Enum
{
    const Cellular = 0;
    const Work = 1;
    const Home = 2;
}
然后可以使用以下方法访问这些值:
PhoneTypes::Work // 1
我建议始终将值设置为整数,然后将它们作为整数存储在数据库中.
基本枚举类具有将所有键和值作为数组获取的方法.该软件包还具有一些其他好处,在这种情况下可能有用,例如验证 - 这样用户就无法向DB添加不存在的值.
还有一个非常方便的发电机.
我希望这对某人有用.
你根本不应该使用枚举。
Laravel 5.1 官方文档指出:
注意:当前不支持使用枚举列重命名表中的列。
当您enum的数据库表中有列时会发生这种情况。无论您是尝试rename 另一列,还是将另一列更改为nullable,都会出现该错误。这是 Doctrine\DBAL 的问题。
请求的未知数据库类型枚举
即使使用 laravel 5.8,问题也没有解决。
我需要补充一点,在将可用选项添加到enum列声明中时,您会遇到同样的问题。
它使我得出一个结论,您应该谨慎使用 enum。甚至你根本不应该使用枚举。
这是将可用选项添加到enum列声明中的难度示例
说你有这个:
Schema::create('blogs', function (Blueprint $table) {
    $table->enum('type', [BlogType::KEY_PAYMENTS]);
    $table->index(['type', 'created_at']);
...
并且您需要提供更多类型
public function up(): void
{
    Schema::table('blogs', function (Blueprint $table) {
        $table->dropIndex(['type', 'created_at']);
        $table->enum('type_tmp', [
            BlogType::KEY_PAYMENTS,
            BlogType::KEY_CATS,
            BlogType::KEY_DOGS,
        ])->after('type');
    });
    DB::statement('update `blogs` as te set te.`type_tmp` = te.`type` ');
    Schema::table('blogs', function (Blueprint $table) {
        $table->dropColumn('type');
    });
    Schema::table('blogs', function (Blueprint $table) {
        $table->enum('type', [
            BlogType::KEY_PAYMENTS,
            BlogType::KEY_CATS,
            BlogType::KEY_DOGS,
        ])->after('type_tmp');
    });
    DB::statement('update `blogs` as te set te.`type` = te.`type_tmp` ');
    Schema::table('blogs', function (Blueprint $table) {
        $table->dropColumn('type_tmp');
        $table->index(['type', 'created_at']);
    });
}
刚刚遇到过类似的问题,对我而言,雄辩的访问者和变异者表现最好。对于这个问题,它将是这样的:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
    /**
    * @var array
    */
    protected $phoneTypes = [
        'Cellular',
        'Home',
        'Work'
    ];
   /**
    * @param int $value
    * @return string|null
    */
    public function getPhoneTypeAttribute($value)
    {
        return Arr::get($this->phoneTypes, $value);
    }
}
请注意,您应该在数据库中保存数值,其中0是单元格,1是家庭,2是工作。其次,明智的做法是在此处使用翻译而不是受保护的财产。
| 归档时间: | 
 | 
| 查看次数: | 53965 次 | 
| 最近记录: |