定制 desmart/laravel-enum 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

desmart/laravel-enum

Composer 安装命令:

composer require desmart/laravel-enum

包简介

PHP enums for Laravel models

README 文档

README

Latest version Tests Software License

Package provides a simple way to use strongly typed enum objects with Laravel models. It utilizes Laravel's custom casting mechanism.

Installation

To install the package via Composer, simply run the following command:

composer require desmart/laravel-enum

Usage

Create an enum class that extends DeSmart\Laravel\Enumeration. Then, simply define all possible values in form of class constants:

class Character extends DeSmart\Laravel\Enumeration
{
    const GOOD = 'good';
    const EVIL = 'evil';
    const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_sometimes_evil';
}

In Laravel model:

class Hero extends Model
{
    /**
     * @var array
     */
    protected $casts = [
        'character' => Character::class,
    ];
}

That's it.

$hero = new Hero(['character' => Character::EVIL]);

dump($hero);
// Hero {#293
//  ...
//  #casts: array:1 [
//    "character" => "Character"
//  ]
// ...
//  #attributes: array:1 [
//    "character" => "evil"
//  ]
// }

dump($hero->character);
// Character {#296
//  -value: "evil"
// }

Enumeration class generation

Package provides make:enum Artisan command for enumeration classes auto-generation. To generate new enum class, run:

php artisan make:enum Character --cases='good,evil,sometimes_good_sometimes_evil'

--cases (or -c) option allows defining available enum cases. Command can be run without that option specified.

Above command will create a new class inside Enums directory:

namespace App\Enums;

use DeSmart\Laravel\Enumeration\Enumeration;

/**
 * @method static Character good()
 * @method static Character evil()
 * @method static Character sometimesGoodSometimesEvil()
 */
class Character extends Enumeration
{
	const GOOD = 'good';
	const EVIL = 'evil';
	const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_sometimes_evil';
}

Changelog

Please see CHANGELOG for more information what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 426
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 5
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固