codelieutenant/laravel-pgenum
Composer 安装命令:
composer require codelieutenant/laravel-pgenum
包简介
Postgres Enums used in Laravel Migrations
README 文档
README
Laravel's answer to Postgres Real Enums
Inspiration
n Laravel’s migration, Enums already exist, yeah? Yes, but not quite. These Enums are not actual Database Enums.
These enums are just a string type with CHECK constraints.
So I said to myself, it would be really nice to use real Postgres enums in Laravel.
Getting started
Installing
composer require codelieutenant/laravel-pgenum
Usage in Migrations
Schema API
This library extends Laravel's Illuminate\Support\Facades\Schema using Illuminate\Support\Traits\Macroable
Schema::createEnum(string $name, ?array $values = null); Schema::createEnumIfNotExists((string $name, ?array $values = null); Schema::addEnumValue(string $type, string $value, ?Direction $direction = null, ?string $otherValue = null, bool $ifNotExists = true); Schema::renameEnumValue(string $type, string $oldName, string $newName); Schema::dropEnum(string $name); Schema::dropEnumIfExists(string $name);
Schema API also works with PHP Enums by default
enum MyEnum: string { case MyValue = 'value'; } // Migrations // Generated SQL // CREATE TYPE my_enum ENUM ('value'); Schema::createEnum(MyEnum::class); // Generated SQL // CREATE TYPE my_enum ENUM ('value'); Schema::dropEnum(MyEnum::class);
Blueprint API
There is only one function for Blueprint enumeration to create a column in table.
// Accepts PHP Enums and Strings \Illuminate\Database\Schema\Blueprint::enumeration(string $name);
Full Migration Example
public function up(): void { // Postgres Enums must be known before Table is created // This is my, `createEnum` must be seperated and before `Schema::create` Schema::createEnum(MyEnum::class); Schema::create('users', function(Blueprint $table) { $table->id(); $table->enumeration(MyEnum::class); }); } public function down(): void { Schema::dropEnum(MyEnum::class); }
Using PHP Enums
Laravel already supports PHP Enums by default, but the nature of this library needs a bit more than what's provided by Laravel by default. PHP Enums (in all forms -> string/int backed, non-backed) are supported, here is the example:
use \Illuminate\Database\Eloquent\Model; use \CodeLieutenant\LaravelPgEnum\Casts\EnumCast; enum Role : string { case Admin = 'admin'; case User = 'user'; } class User extends Model { // For Laravel < 11.0 use $casts property public function casts(): array { return [ // This is supported by default // as long as PHP Enum is backed by string 'role' => Role::class, // For non-backed/int backed PHP Enums // custom converter is needed, this also // can be applied to string backed enums (but not needed) // This is due to the limitation of Postgres Enums // as it requires values to be string named, and not INTs // Using Role::class as example, can be any other PHP Enum 'non_string_backed_enum' => EnumCast::class ':' . Role::class ]; } }
codelieutenant/laravel-pgenum 适用场景与选型建议
codelieutenant/laravel-pgenum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.9k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「postgres」 「laravel」 「enums」 「pg-enums」 「postgres enums」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codelieutenant/laravel-pgenum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codelieutenant/laravel-pgenum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codelieutenant/laravel-pgenum 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enums for Laravel Enso
Shared backed enums classifying HTML elements by category (block, inline, list, table, void, metadata, root) for the UI Awesome ecosystem.
Helpers for handling enums and enum translations in Laravel
A Laravel package that adds extra power to your PHP enums, and lets you use them in your frontend with type definitions.
Bundle for Doctrine enumerations extension for Postgres
Doctrine extension to manage enumerations in PostgreSQL
统计信息
- 总下载量: 1.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2024-04-20