9951 explained code solutions for 126 technologies


php-laravelHow do I use Enum in Laravel with PHP?


Enum is a data type that allows you to define a set of named constants. It can be used in Laravel with PHP to create an enumeration of values.

Here is an example of how to use Enum in Laravel with PHP:

<?php

namespace App;

use MyCLabs\Enum\Enum;

class UserType extends Enum
{
    const ADMIN = 'admin';
    const USER = 'user';
}

$userType = new UserType(UserType::USER);
echo $userType;

The output of this code will be user.

The code consists of the following parts:

  1. A namespace statement to specify the namespace of the Enum class.
  2. An use statement to import the Enum class from the MyCLabs\Enum package.
  3. A class extending the Enum class to define the constants.
  4. An instance of the UserType class initialized with the constant USER.
  5. An echo statement to output the value of the constant.

For more information on using Enum in Laravel with PHP, see the following links:

Edit this code on GitHub