DEV Community

Cover image for πŸš€ Introducing Laravel ObjectId β€” The Fastest MongoDB-Style Identifier for Laravel Models
Hamada Habib
Hamada Habib

Posted on

πŸš€ Introducing Laravel ObjectId β€” The Fastest MongoDB-Style Identifier for Laravel Models

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:


🧩 The Ecosystem

The Laravel package is built on top of our core PHP library:

Both packages are open-source under the MIT license, created by WooServ Labs.


⚑ Installation

composer require wooserv/laravel-objectid
Enter fullscreen mode Exit fullscreen mode

🧱 Usage

Model Example

use WooServ\LaravelObjectId\Concerns\HasObjectIds;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasObjectIds;
}
Enter fullscreen mode Exit fullscreen mode

Migration Example

Schema::create('posts', function (Blueprint $table) {
    $table->objectId(); // Creates a 24-char string primary key
    $table->string('title');
    $table->timestamps();
});
Enter fullscreen mode Exit fullscreen mode

Generate IDs Anywhere

$id = objectid(); // e.g. 6730b6a0d8a28f890b7c9f40
Enter fullscreen mode Exit fullscreen mode

πŸ”¬ 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 πŸš€
Enter fullscreen mode Exit fullscreen mode

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)
----------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

βš”οΈ 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


⭐ If you like it β€” give it a Star on GitHub and share it with your fellow Laravel devs!

Top comments (0)