Works seamlessly with MySQL, MariaDB, and PostgreSQL β no MongoDB required.
Every Laravel developer has been there β you start a new project, define your migrations, and by default, your models use auto-incrementing IDs. It works fine... until your app grows, you need distributed systems, API integrations, or microservices. Suddenly, those integer IDs start to look like a limitation.
Thatβs where Laravel ObjectId comes in β a drop-in, ultra-fast, globally unique identifier system inspired by MongoDBβs ObjectIds, designed for MySQL, MariaDB, and PostgreSQL β no MongoDB required.
π‘ Why ObjectId?
Unlike UUIDs or ULIDs, ObjectIds are compact 12-byte identifiers that encode timestamp, randomness, and a counter β making them **sortable, lightweigβ¦
Works seamlessly with MySQL, MariaDB, and PostgreSQL β no MongoDB required.
Every Laravel developer has been there β you start a new project, define your migrations, and by default, your models use auto-incrementing IDs. It works fine... until your app grows, you need distributed systems, API integrations, or microservices. Suddenly, those integer IDs start to look like a limitation.
Thatβs where Laravel ObjectId comes in β a drop-in, ultra-fast, globally unique identifier system inspired by MongoDBβs ObjectIds, designed for MySQL, MariaDB, and PostgreSQL β no MongoDB required.
π‘ Why ObjectId?
Unlike UUIDs or ULIDs, ObjectIds are compact 12-byte identifiers that encode timestamp, randomness, and a counter β making them sortable, lightweight, and unique across systems.
In numeric terms, theyβre up to 3Γ faster to generate and smaller in size, which directly improves database performance and indexing.
β Works natively with MySQL, MariaDB, and PostgreSQL π« No MongoDB driver or extension required
𧬠How ObjectId Works Internally
ObjectIds are 12-byte (96-bit) identifiers consisting of four key parts:
| Segment | Size | Description |
|---|---|---|
| Timestamp | 4 bytes | UNIX epoch seconds β makes IDs sortable by creation time |
| Machine Identifier | 5 bytes | Randomly generated, unique per host |
| Process ID | 2 bytes | Uniquely identifies the generating process |
| Counter | 3 bytes | Incrementing counter initialized randomly per process |
β‘οΈ Total = 4 + 5 + 2 + 3 = 12 bytes = 24 hex characters
This design ensures uniqueness without any central generator and preserves chronological order, making it ideal for distributed systems.
For more details, check:
- MongoDBβs ObjectId Reference
- Perconaβs Deep Dive into ObjectIds
- Stack Overflow breakdown of ObjectId structure
π§© The Ecosystem
The Laravel package is built on top of our core PHP library:
- πΉ wooserv/php-objectid: a pure PHP implementation of ObjectIds.
- πΉ wooserv/laravel-objectid: Laravel integration with automatic model ID assignment and migration macros.
Both packages are open-source under the MIT license, created by WooServ Labs.
β‘ Installation
composer require wooserv/laravel-objectid
π§± Usage
Model Example
use WooServ\LaravelObjectId\Concerns\HasObjectIds;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasObjectIds;
}
Migration Example
Schema::create('posts', function (Blueprint $table) {
$table->objectId(); // Creates a 24-char string primary key
$table->string('title');
$table->timestamps();
});
Generate IDs Anywhere
$id = objectid(); // e.g. 6730b6a0d8a28f890b7c9f40
π¬ Benchmark Results
Laravel ObjectId Performance (10,000 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 π
Database Insert Benchmark (1,000 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)
----------------------------------------------------------
βοΈ ObjectId vs UUID vs ULID
| Feature | ObjectId | UUID | ULID |
|---|---|---|---|
| Length | 24 chars | 36 chars | 26 chars |
| Bytes | 12 | 16 | 16 |
| Sortable | β (timestamp prefix) | β | β |
| Randomness | β | β | β |
| Readability | π’ Compact | π΄ Long | π’ Fair |
| Speed | π Fastest | π’ Slowest | β‘ Medium |
| Works with MySQL/MariaDB/PostgreSQL | β | β | β |
| Requires MongoDB | β | β | β |
Verdict: ObjectId is the best balance between compactness, performance, and chronological order β and itβs fully compatible with standard SQL databases.
π§ Why Developers Love It
- β Automatic assignment β no need to manually generate IDs.
- βοΈ Migration macro
$table->objectId()β clean and intuitive. - π§© Framework-agnostic core β works outside Laravel too.
- π¦ Compact storage β saves DB space compared to UUID.
- π Sortable by creation time β built-in timestamp encoding.
- π§° Database-compatible β works with MySQL, MariaDB, and PostgreSQL seamlessly.
π Open Source Philosophy
Both packages are released under the MIT License and maintained by WooServ Labs, a collective of developers building open, high-performance PHP tooling for modern web apps.
We believe that open-source should be:
- Simple to install.
- Fun to use.
- Fast by default.
If you share that philosophy β star the repo, contribute, or just spread the word π«
π Resources
- π Laravel ObjectId on GitHub
- π PHP ObjectId on GitHub
- π¦ Packagist: wooserv/laravel-objectid
- π¦ Packagist: wooserv/php-objectid
- π MongoDB ObjectId Reference
β If you like it β give it a Star on GitHub and share it with your fellow Laravel devs!