承接 wooserv/laravel-objectid 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

wooserv/laravel-objectid

最新稳定版本:v1.0.0

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.

Tests Packagist License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-09