【问题标题】:How to show to handle enums in Laravel?如何显示在 Laravel 中处理枚举?
【发布时间】:2021-05-26 18:51:45
【问题描述】:

我喜欢将所有这些值都放在一个中心位置。例如,我可能有一个如下所示的枚举:

$mobies = [
    'APPLE' => "Apple",
    'SUM' => "Sumsung",
    'LG' => "LG",
];

我想在我的视图/模板中使用:

迁移表

Schema::create('products', function (Blueprint $table) {
    $table->id;
    $table->enum('mobiles',['apple','sumsung','lg']);
    ...
});

config/enums.php

<?php

return [
    'mobiles' => [
        'APPLE' => "Apple",
        'SUM' => "Sumsung",
        'LG' => "LG",
    ];
];

index.blade.php

{{ config('enums.reportableTitle') }}

保存在数据库中时如何在刀片中显示。

【问题讨论】:

  • 有人可以回答我的问题吗?

标签: php laravel


【解决方案1】:

让我们从config/enums.php开始

<?php

return [
    'mobiles' => [
        'APPLE' => "Apple",
        'SUM' => "Sumsung",
        'LG' => "LG",
    ]
];

您可以使用配置助手config('enums.mobiles')在任何地方重复使用它

您可以通过 array_values() 仅检索其值来在迁移表中使用它

Schema::create('products', function (Blueprint $table) {
    $table->id;
    $table->enum('mobiles', array_values(config('enums.mobiles')));
    ...
});

在您的刀片文件中,您还可以使用配置助手,如

{{ config('enums.mobiles')['APPLE'] }}

【讨论】:

    猜你喜欢
    • 2014-10-31
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多