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
composer require wooserv/laravel-objectidThis package is auto-discovered by Laravel. No manual provider registration needed.
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. 6730b6a0d8a28f890b7c9f40The 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$id = objectid(); // returns 24-char hex ObjectId string| Feature | ObjectId | UUID | ULID |
|---|---|---|---|
| Sortable | ✅ Yes | ❌ No | ✅ Yes |
| Length | 24 chars | 36 chars | 26 chars |
| Contains Timestamp | ✅ Yes | ❌ No | ✅ Yes |
| Index Friendly | ✅ Yes | ✅ Yes | |
| Collision Chance | 🔒 Extremely Low | 🔒 Very Low | 🔒 Very Low |
composer testRuns a full PHPUnit suite using an in-memory SQLite database.
All benchmarks were executed on PHP 8.4 using in-memory SQLite and 10000 iterations per test on a local machine.
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 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.
| 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.
MIT © WooServ