alesitom/hybrid-id-laravel
Composer 安装命令:
composer require alesitom/hybrid-id-laravel
包简介
Laravel integration for HybridId — Eloquent trait, service provider, and config
README 文档
README
Laravel integration for HybridId — compact, time-sortable unique IDs as a drop-in UUID replacement for Eloquent models.
Installation
composer require alesitom/hybrid-id-laravel
The service provider is auto-discovered. No manual registration needed.
Quick Start
Add the HasHybridId trait to any model:
use HybridId\Laravel\HasHybridId; class User extends Model { use HasHybridId; protected static string $idPrefix = 'usr'; }
That's it. New models automatically get a HybridId on creation:
$user = User::create(['name' => 'Jane']); $user->id; // usr_0VBFDQz4CYRtntu09sbf
Migration
Your primary key column must be a string, not an auto-incrementing integer:
Schema::create('users', function (Blueprint $table) { $table->string('id', 29)->collation('ascii_bin')->primary(); // ... other columns $table->timestamps(); });
Use ascii_bin collation on MySQL/MariaDB to preserve case-sensitive ordering. See core docs for details.
Configuration
Publish the config file:
php artisan vendor:publish --tag=hybrid-id-config
This creates config/hybrid-id.php:
return [ 'profile' => env('HYBRID_ID_PROFILE', 'standard'), 'node' => env('HYBRID_ID_NODE'), 'require_explicit_node' => (bool) env('HYBRID_ID_REQUIRE_NODE', false), 'blind' => (bool) env('HYBRID_ID_BLIND', false), 'blind_secret' => env('HYBRID_ID_BLIND_SECRET'), ];
Set HYBRID_ID_REQUIRE_NODE=1 in production to enforce explicit node assignment.
Blind Mode
Enable blind mode to HMAC-hash timestamps and node info, making creation time unextractable:
HYBRID_ID_BLIND=true HYBRID_ID_BLIND_SECRET=base64encodedvalue...
Generate a secret: php -r "echo base64_encode(random_bytes(32)) . PHP_EOL;"
See Blind Mode docs for details.
Dependency Injection
The service provider binds IdGenerator as a singleton. Inject it anywhere:
use HybridId\IdGenerator; class OrderService { public function __construct( private readonly IdGenerator $idGenerator, ) {} public function createOrder(): Order { return Order::create([ 'id' => $this->idGenerator->generate('ord'), 'status' => 'pending', ]); } }
Prefixes
Set $idPrefix on your model for Stripe-style self-documenting IDs:
class Order extends Model { use HasHybridId; protected static string $idPrefix = 'ord'; } class Invoice extends Model { use HasHybridId; protected static string $idPrefix = 'inv'; }
Omit $idPrefix for unprefixed IDs.
How It Works
HasHybridIdhooks into Eloquent'screatingevent- Sets
$keyType = 'string'and$incrementing = falseautomatically - If the model's primary key is empty at creation time, generates a HybridId
- If the primary key is already set (e.g., manual assignment), it is not overwritten
- The generator instance is resolved from the container (singleton)
Requirements
- PHP 8.3, 8.4, or 8.5
- Laravel 11 or 12
- alesitom/hybrid-id ^4.1 (installed automatically)
License
MIT
alesitom/hybrid-id-laravel 适用场景与选型建议
alesitom/hybrid-id-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「eloquent」 「unique-id」 「uuid-alternative」 「time-sortable」 「hybrid-id」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alesitom/hybrid-id-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alesitom/hybrid-id-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alesitom/hybrid-id-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
Compact, time-sortable unique ID generator for PHP. Drop-in UUID/ULID alternative with base62 encoding, Stripe-style prefixes, and configurable entropy profiles.
Alfabank REST API integration
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-16