Home  >  Article  >  PHP Framework  >  Using enums in Laravel

Using enums in Laravel

Guanhui
Guanhuiforward
2020-06-12 18:10:393217browse

Using enums in Laravel

I'm a big fan of enums. Recently, I worked for a company that uses C#, where enums are used extensively, and I'm used to being able to find them when I can't find them. and miss them.

I use them for anything that has a limited set of possible options, such as the day of the week, the status of an order, or, as in the example below, the user type.

Using enumerations has several benefits:

- Reduces errors caused by transposition or incorrectly entered numbers.

- Makes it easy to change values ​​in the future.

- Makes code easier to read, which means bugs are less likely to sneak into it.

- Ensure Forward Compatibility When using enumerations, you can greatly reduce the chance that your code will fail if someone changes the value corresponding to the member name in the future.

PHP itself does not support enumerations, but it is quite easy to achieve equivalent effects using constants in classes. Additionally, I created a Laravel package called laravel-enum. It allows you to access helper functions such as listing keys and values, attaching descriptions to values, and validating requests that expect enumeration values.

This guide walks through the process of installing the Laravel package including examples of usage and best practices.

Installation package

You can run the following command in the terminal through composer to install the package:

$ composer require bensampo/laravel-enum

If you are using Laravel lower than 5.5 version, you need to add the service provider to config/app.php.

'BenSampo\Enum\EnumServiceProvider'

Create the first enumeration

We will create an enumeration for the user type In our sample application, users can be one of three user types: Administrator, Paid Member, Member.

The package contains generators for creating enumerations, so you can run the following command to create an enumeration named UserType The file will be created in "app/Enums/UserType.php"

php artisan make:enum UserType

You will see a certain amount of scaffolding in this file. Near the top of the file, the list of possible options is defined as constants. These constant values ​​are stored in the database, so I found it best to use integers, but there is no restriction on using integers as long as each value is unique.

The options in this example look like this:

const Administrator = 0;
const PaidMember = 1;
const Member = 2;

Store the value in the database

Now we have an enumeration with some possibilities, and can start using it. When migrating the user table, you can add the following.

$table->tinyInteger('type')->unsigned()->default(UserType::Member);

Because null is not an option for the enumeration, we need to set a default value for it. In this example, it is necessary to assume that the default user will become a standard member.

Make sure the top of the file contains the use statement for this enumeration.

use App\Enums\UserType;

Using enumerations in operations

Since our current user model has a property of type, we can access it and compare it to the enum value. This is the real benefit of enums, and why I like them so much. Take a look at the usage examples and possible alternatives below.

if ($user->type === UserType::PaidMember) {
    // 在这里只是做一些付费会员的事情.
}

If we don’t use enumerations, we may have code similar to the following:

if ($user->type === 1) { //What does this 1 mean??
// ...
}
if ($user->type === 'PaidMember') { // Why the hell is this a string again?

The above is the detailed content of Using enums in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Laravel view compositorNext article:Laravel view compositor