Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

php - Where to put/how to handle enums in Laravel?

Laravel has a <select> form helper which takes as input a dictionary. I like to keep the values for all of these in a central place. For example, I might have an enum that looks like this:

$phoneTypes = [
    'CELL' => "Cellular",
    'HOME' => "Home",
    'WORK' => "Work",
];

Which I want to use in both my view/template, and in the database:

Schema::create('customers', function (Blueprint $table) {
    $table->increments('id');
    $table->enum('pri_phone_type',array_keys($phoneTypes));
    ...
});
  1. Is there a recommended place to put these?
  2. Can I make them global so I can access them easily in all my views?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

2021 Update: PHP 8.1 is bringing built-in support for enums, finally! See more here: https://stitcher.io/blog/php-enums

Original answer below:


You have several options for handling enums. Before we look at a few though, I would first strongly encourage you not to use the DB enum column type.

Database enums are problematic for a number of reasons. I suggest reading this article for example:

http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/

So with that let's look at a few other options.

Using Laravel config

Since you're using Laravel, one very simple option is to stick an array of options in a config file.

Say you create a new file config/enums.php with the following:

return [
    'phone_types' => [
        'CELL' => "Cellular",
        'HOME' => "Home",
        'WORK' => "Work",
    ]
];

You can now access config('enums.phone_types') anywhere in your code, including your Blade template.

Using a PHP package

@Banford's answer shows how to do basic enum-type behavior with class constants. If you like that approach, I recommend looking at this article and package which builds on this concept to provide strongly type enums:

https://stitcher.io/blog/php-enums

https://github.com/spatie/enum

You would create a class like this:

/**
 * @method static self cell()
 * @method static self home()
 * @method static self work()
 */
class PhoneTypes extends Enum
{
}

And now you can call PhoneTypes::home() in your app. Check out the documentation for that package to see how you can create a map of values, if you want.

Using DB relationships

If you really want to manage your options in the database, I'd create a separate phone_types database table and create a relationship with your customers table. This is still a much better option than using enum column type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...