alesitom/hybrid-id-laravel 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

alesitom/hybrid-id-laravel

最新稳定版本:v2.2.0

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.

Tests Coverage PHPStan

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

  • HasHybridId hooks into Eloquent's creating event
  • Sets $keyType = 'string' and $incrementing = false automatically
  • 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

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固