wooserv/laravel-objectid
Composer 安装命令:
composer require wooserv/laravel-objectid
包简介
Elegant, fast ObjectId generator for Laravel models with automatic ID assignment, schema macro, and helper function.
关键字:
README 文档
README
Elegant, fast ObjectId generator for Laravel models — with automatic ID assignment, migration macro, and helper function.
Laravel ObjectId brings the power and efficiency of MongoDB-style ObjectIds to your Eloquent models — with no database dependency.
It’s a drop-in, ultra-fast unique ID system that fits seamlessly into Laravel’s model lifecycle.
With this package, you can:
- Automatically assign 24-character hex ObjectIds to your models.
- Use
$table->objectId()directly in your migrations.- Generate IDs anywhere using the global
objectid()helper.- Enjoy compact, sortable, timestamp-encoded identifiers — 3× faster than UUIDs.
Built for performance, readability, and developer happiness.
Perfect for:
- Large-scale Laravel apps
- Multi-database systems
- UUID/ULID replacements
- Caching and indexing optimization
Installation
composer require wooserv/laravel-objectid
This package is auto-discovered by Laravel. No manual provider registration needed.
Usage
1. Model
use WooServ\LaravelObjectId\Concerns\HasObjectIds; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasObjectIds; }
Now every new record gets a unique ObjectId automatically:
$post = Post::create(['name' => 'Hello World']); echo $post->id; // e.g. 6730b6a0d8a28f890b7c9f40
2. Migration Macro
The service provider automatically adds a new macro to the schema builder:
Schema::create('posts', function (Blueprint $table) { $table->objectId(); // Creates string(24) primary key $table->string('name'); $table->timestamps(); });
Optionally:
$table->objectId('uuid', false); // custom column, not primary
3. Helper Function
$id = objectid(); // returns 24-char hex ObjectId string
Why Laravel ObjectId?
| Feature | ObjectId | UUID | ULID |
|---|---|---|---|
| Sortable | ✅ Yes | ❌ No | ✅ Yes |
| Length | 24 chars | 36 chars | 26 chars |
| Contains Timestamp | ✅ Yes | ❌ No | ✅ Yes |
| Index Friendly | ✅ Yes | ⚠️ Larger Indexes | ✅ Yes |
| Collision Chance | 🔒 Extremely Low | 🔒 Very Low | 🔒 Very Low |
Testing
composer test
Runs a full PHPUnit suite using an in-memory SQLite database.
⚡️ Benchmark Results
All benchmarks were executed on PHP 8.4 using in-memory SQLite and 10000 iterations per test on a local machine.
ObjectId Generation Speed
Laravel ObjectId Benchmark (10000 iterations)
----------------------------------------------------------
ObjectId : 0.412 µs per ID
objectid() helper : 0.417 µs per ID
UUID : 1.283 µs per ID
ULID : 1.147 µs per ID
----------------------------------------------------------
Fastest: ObjectId
Result: ObjectId is roughly 3× faster than UUID and ~2.7× faster than ULID.
Database Insert Performance
Database Insert Benchmark (1000 inserts)
----------------------------------------------------------
ObjectId : 14.78 ms total (0.015 ms/insert)
UUID : 15.48 ms total (0.015 ms/insert)
ULID : 15.17 ms total (0.015 ms/insert)
----------------------------------------------------------
Result: Real-world insert performance is effectively identical across ID types, but ObjectId maintains slightly lower overhead during generation and indexing.
Summary
| Metric | ObjectId | UUID | ULID |
|---|---|---|---|
| Generation Speed | 🥇 Fastest | ⚪ Slow | ⚪ Medium |
| Insert Speed | ⚡ Very Fast | ⚡ Very Fast | ⚡ Very Fast |
| Length | 24 chars | 36 chars | 26 chars |
| Sortable | ✅ Yes | ❌ No | ✅ Yes |
| DB Index Size | 🔹 Small | 🔸 Large | 🔹 Small |
| Human Readable | ⚪ Hex | ⚪ Hyphenated | ⚪ Base32 |
| Timestamp Embedded | ✅ Yes | ❌ No | ✅ Yes |
Conclusion:
Laravel ObjectId provides faster generation, compact indexes,
and timestamp-friendly IDs — ideal for large-scale Laravel applications.
License
MIT © WooServ
wooserv/laravel-objectid 适用场景与选型建议
wooserv/laravel-objectid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 423 次下载、GitHub Stars 达 51, 最近一次更新时间为 2025 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「mongodb」 「migration」 「uuid」 「laravel」 「Unique ID」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wooserv/laravel-objectid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wooserv/laravel-objectid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wooserv/laravel-objectid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Generates Symfony2 documents, forms and CRUD
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Lets you add foreign keys in a swift! Foreign key adder in laravel migration.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 423
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 51
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-09