PHPackages                             dive-be/laravel-snowflake - 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. [Database &amp; ORM](/categories/database)
4. /
5. dive-be/laravel-snowflake

ActiveLibrary[Database &amp; ORM](/categories/database)

dive-be/laravel-snowflake
=========================

Generate identifiers using Twitter Snowflake

1.3.0(2y ago)8776MITPHPPHP ~8.3

Since Apr 10Pushed 2y agoCompare

[ Source](https://github.com/dive-be/laravel-snowflake)[ Packagist](https://packagist.org/packages/dive-be/laravel-snowflake)[ Docs](https://github.com/dive-be/laravel-snowflake)[ RSS](/packages/dive-be-laravel-snowflake/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (9)Versions (5)Used By (0)

[![Social Card of Laravel Dry Requests](https://github.com/dive-be/laravel-snowflake/raw/master/art/socialcard.png?raw=true)](https://github.com/dive-be/laravel-snowflake/blob/master/art/socialcard.png?raw=true)

❄️ Generate IDs using Twitter Snowflake
=======================================

[](#️-generate-ids-using-twitter-snowflake)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1413e66d2d789c236c05dc218d1f04c4c70238294f931227ff1c7330c4a2dcfe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646976652d62652f6c61726176656c2d736e6f77666c616b652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dive-be/laravel-snowflake)

This package assists you in creating [Snowflake identifiers](https://en.wikipedia.org/wiki/Snowflake_ID) for your Eloquent models.

It is a Laravel wrapper for [godruoyi/php-snowflake](https://github.com/godruoyi/php-snowflake).

What problem does this package solve?
-------------------------------------

[](#what-problem-does-this-package-solve)

Please refer to the [original library](https://github.com/godruoyi/php-snowflake) for more information regarding Snowflakes.

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

[](#installation)

You can install the package via composer:

```
composer require dive-be/laravel-snowflake
```

You can publish the config file with:

```
php artisan vendor:publish --provider="Dive\Snowflake\ServiceProvider" --tag="config"
```

This is the contents of the published config file:

```
return [

    /**
     * Set this value to today when starting a new app.
     * You will have 69 years before you run out of snowflakes.
     */
    'start_date' => '2022-04-10',
];
```

Usage
-----

[](#usage)

> ⚠️ Use a high-performing cache driver such as `Redis` to ensure rapid ID generation.

> ❗️ Do **not** use an ephemeral cache driver such as `array` in production!

### Migrations

[](#migrations)

- Use `snowflake` to define a Snowflake column
- Use `foreignSnowflake` to reference another Snowflake (alias for `foreignId`)

```
Schema::table('products', static function (Blueprint $table) {
    $table->snowflake();
    $table->foreignSnowflake('variant_id')->constrained();
});
```

### Models

[](#models)

Use the `HasSnowflake` trait in your Eloquent models:

```
class Product extends Model
{
    use HasSnowflake;
}
```

### Manual generation

[](#manual-generation)

You have a couple of options if you'd like to generate your Snowflake identifiers manually:

```
Snowflake::id(); // Facade
snowflake(); // Helper
app('snowflake'); // Service Locator

// Dependency Injection
public function __construct(\Godruoyi\Snowflake\Snowflake $snowflake) {}
```

### 📣 Note on JavaScript compatibility

[](#-note-on-javascript-compatibility)

While JavaScript itself actually [supports `BigInt`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt), the `JSON` standard does not.

```
JSON.parse("{\"a\":10n}")
// Uncaught SyntaxError: Unexpected token n in JSON at position 7
```

Therefore, to make sure the identifiers are not truncated while deserializing them on the front-end using `JSON.parse` and alike, the package will automatically cast the models' `id` field to `string`.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Muhammed Sari](https://github.com/mabdullahsari)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity66

Established project with proven stability

 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 ~234 days

Total

4

Last Release

788d ago

PHP version history (3 changes)1.0.0PHP ^8.1

1.2.0PHP ~8.2

1.3.0PHP ~8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/a265853c8db7c699f64f523ee782dc5ec095bfb456379222d1b0b97d90e4c735?d=identicon)[dive](/maintainers/dive)

---

Top Contributors

[![dive-michiel](https://avatars.githubusercontent.com/u/2650975?v=4)](https://github.com/dive-michiel "dive-michiel (1 commits)")

---

Tags

laraveleloquentsnowflakedive

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/dive-be-laravel-snowflake/health.svg)

```
[![Health](https://phpackages.com/badges/dive-be-laravel-snowflake/health.svg)](https://phpackages.com/packages/dive-be-laravel-snowflake)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)

PHPackages © 2026

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