PHPackages                             attla/ulid - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. attla/ulid

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

attla/ulid
==========

Universally Unique Lexicographically Sortable Identifier (ULID) implementation for Attla and Laravel.

v1.2.3(1y ago)2190MITPHPPHP &gt;=7.3

Since Nov 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/attla/ulid)[ Packagist](https://packagist.org/packages/attla/ulid)[ Docs](https://github.com/attla/ulid)[ RSS](/packages/attla-ulid/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (7)Dependencies (3)Versions (8)Used By (0)

Attla ULID
==========

[](#attla-ulid)

[![License](https://camo.githubusercontent.com/891419a00e04aa0e311068fa8a04eec92cab4f7026c76278279bf2a1da50e578/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d6c69676874677265792e737667)](LICENSE)[![Latest Stable Version](https://camo.githubusercontent.com/3dd3a84a63e7c377f8bdfcb42e5eec323b965ad738144420dd903a2c39390664/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6174746c612f756c6964)](https://packagist.org/packages/attla/ulid)[![Total Downloads](https://camo.githubusercontent.com/f454f29a839008620892d2120bc69ad8bb061a8e7c606c48cd74c92b8109885e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6174746c612f756c6964)](https://packagist.org/packages/attla/ulid)

A PHP port of [ulid/javascript](https://github.com/ulid/javascript) with some minor improvements.

Universally Unique Lexicographically Sortable Identifier
--------------------------------------------------------

[](#universally-unique-lexicographically-sortable-identifier)

UUID can be suboptimal for many uses-cases because:

- It isn't the most character efficient way of encoding 128 bits of randomness
- UUID v1/v2 is impractical in many environments, as it requires access to a unique, stable MAC address
- UUID v3/v5 requires a unique seed and produces randomly distributed IDs, which can cause fragmentation in many data structures
- UUID v4 provides no other information than randomness which can cause fragmentation in many data structures

Instead, herein is proposed ULID:

- 128-bit compatibility with UUID
- 1.21e+24 unique ULIDs per millisecond
- Lexicographically sortable!
- Canonically encoded as a 26 character string, as opposed to the 36 character UUID
- Uses Crockford's base32 for better efficiency and readability (5 bits per character)
- Case insensitive
- No special characters (URL safe)
- Monotonic sort order (correctly detects and handles the same millisecond)

You can read more [here](https://github.com/ulid/javascript)

### What are the benefits?

[](#what-are-the-benefits)

1. With distributed systems you can be pretty confident that the primary key’s will never collide.
2. When building a large scale application when an auto increment primary key is not ideal.
3. It makes replication trivial (as opposed to int’s, which makes it REALLY hard)
4. Safe enough doesn’t show the user that you are getting information by id, for example `https://example.com/item/10`

Installation
------------

[](#installation)

```
composer require attla/ulid
```

Usage
-----

[](#usage)

```
use Attla\Ulid\Factory as UlidFactory;

$ulid = UlidFactory::generate();
echo $ulid; // 01B8KYR6G8BC61CE8R6K2T16HY
echo $ulid->generate(); // 01B8KYR6G8BC61CE8R6K2T16HZ

// Or if you prefer a lowercased output
$ulid = UlidFactory::generate(true);
echo $ulid->get(); // 01b8kyr6g8bc61ce8r6k2t16hy

// If you need the timestamp from an ULID instance
$ulid = UlidFactory::generate();
echo $ulid->toTimestamp(); // 1561622862

// You can also generate a ULID for a specific UNIX-time in milliseconds
$ulid = UlidFactory::fromTimestamp(1593048767015);
// or with a lower cased output: $ulid = UlidFactory::fromTimestamp(1593048767015, true);
echo $ulid->toString(); // 01EBMHP6H7TT1Q4B7CA018K5MQ
```

Use the methods `get()` or `toString()` to get the ULID as a string on ULID instances.

### Migrations

[](#migrations)

When using the migration you should change $table-&gt;increments('id') or $table-&gt;id() to:

```
$table->ulid();
```

> Simply, the schema seems something like this.

```
Schema::create('items', function (Blueprint $table) {
  $table->ulid();
  ....
  ....
  $table->timestamps();
});
```

If the related model is using an ULID, the column type should reflect that also.

```
Schema::create('items', function (Blueprint $table) {
  $table->ulid();
  ....
  // related model that uses ULID
  $table->foreignUlid('category_id');
  ....
  $table->timestamps();
});
```

The ULID blueprint parameter is optional. But below is an example of how to use it.

```
Schema::create('categories', function (Blueprint $table) {
  $table->ulid($ulidLength);
  ....
  // related model that uses ULID
  $table->foreignUlid($column, $foreignColumn, $foreignTable, $ulidLength);
  ....
  $table->timestamps();
});
```

Models
------

[](#models)

To set up a model to use ULID, simply use the HasUlid trait.

```
use Illuminate\Database\Eloquent\Model;
use Attla\Ulid\HasUlid;

class Item extends Model
{
  use HasUlid;
}
```

### Controller

[](#controller)

When you create a new instance of a model which uses ULIDs, this package will automatically add ULID as id of the model.

```
// 'HasUlid' trait will automatically generate and assign id field.
$item = Item::create(['name' => 'Awesome item']);
echo $item->id;
// 01brh9q9amqp7mt7xqqb6b5k58
```

### Testing

[](#testing)

```
composer test
```

### Benchmark

[](#benchmark)

```
composer benchmark
```

License
-------

[](#license)

This package is licensed under the [MIT license](LICENSE) © [Octha](https://octha.com).

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~167 days

Recently: every ~250 days

Total

7

Last Release

657d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/682727?v=4)[Andre Goncalves](/maintainers/nicolau)[@nicolau](https://github.com/nicolau)

---

Top Contributors

[![nicxlau](https://avatars.githubusercontent.com/u/17990891?v=4)](https://github.com/nicxlau "nicxlau (12 commits)")

---

Tags

attlaulidulid-generatoruuiduuid-alternativelaraveluuiduliduuid-alternativeattla

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/attla-ulid/health.svg)

```
[![Health](https://phpackages.com/badges/attla-ulid/health.svg)](https://phpackages.com/packages/attla-ulid)
```

###  Alternatives

[webpatser/laravel-uuid

Laravel integration for webpatser/uuid - High-performance drop-in UUID replacements (15% faster than Ramsey). Provides Str macros, HasUuids trait, facades, and casts. RFC 4122/9562 compliant.

1.8k17.3M129](/packages/webpatser-laravel-uuid)[riipandi/laravel-optikey

Use UUID, Ulid, or nanoid as optional or primary key in Laravel.

429.1k](/packages/riipandi-laravel-optikey)[madewithlove/laravel-nova-uuid-support

Adds uuid and other string identifier support to Laravel Nova

28132.9k](/packages/madewithlove-laravel-nova-uuid-support)[michalsn/codeigniter4-uuid

UUID and ULID package for CodeIgniter 4 with support for Model.

4728.7k4](/packages/michalsn-codeigniter4-uuid)[identifier/identifier

Common Interfaces and Factories for Identifiers

3226.2k1](/packages/identifier-identifier)[infocyph/uid

UUID (RFC 4122 + Unofficial/Draft), ULID, Snowflake ID, Sonyflake ID, TBSL (library exclusive) generator!

105.1k](/packages/infocyph-uid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
