承接 cmariesprp/laravel-snowflake 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cmariesprp/laravel-snowflake

Composer 安装命令:

composer require cmariesprp/laravel-snowflake

包简介

Laravel implementation for Snowflake IDs

README 文档

README

A Laravel wrapper of PHP Snowflake to provide a simple, opinionated implementation of Snowflake IDs for your eloquent models.

Static Analysis Tests Laravel Version PHP Version

Introduction

The laravel-snowflake package provides a seamless way to generate unique, time-ordered, distributed IDs within your Laravel application. Unlike traditional auto-incrementing IDs, Snowflake IDs offer several advantages: they eliminate ID collisions across distributed systems, enhance horizontal scalability, and prevent predictable sequential exposure of database records. Compared to UUIDs and ULIDs, Snowflake IDs are shorter, visually appealing, and easier to write out, making them ideal for public identifiers. This package integrates smoothly with Laravel, offering microsecond precision while ensuring high-performance, conflict-free ID generation across multiple clusters and workers. 🚀

Installation

composer require bradietilley/laravel-snowflake

Documentation

Preparing your schema:

You'll want to make sure that your model's primary key does not autoincrement. Autoincrement is automatically added when you use $table->id(); so go ahead and switch this out:

-$table->id();
+$table->bigInteger('id')->unsigned()->primary();

Integrating with you models:

Next you'll want to add the HasSnowflake trait to your models. This trait this will handle all aspects of a snowflake ID including:

  • Automatically setting the id to a Snowflake ID
  • Configuring the cast for id to string
  • Disabling increments on the model
  • Configuring the keyType to string

This can be done as simple as:

use BradieTilley\Snowflakes\Eloquent\HasSnowflake;

class SomeModel extends Model
{
    use HasSnowflake;
}

You're all set.

$model = SomeModel::create();
$model->id; // 9348975348573485734

Concurrency

The package includes a LaravelSequenceResolver for PHP Snowflake, which utilizes a cache repository to manage the generation of multiple concurrent IDs within the same microsecond.

You can configure various cache-related options, such as:

  • The cache store (snowflakes.sequencing.store)
  • Cache prefix (snowflakes.sequencing.prefix)
  • Cache lock expiry (snowflakes.sequencing.expiry)
  • Cache lock wait time (snowflakes.sequencing.wait)

These settings allow for proper handling of concurrent ID generation in distributed environments.

Testing

In unit testing scenarios, you may want to generate sequential, predictable IDs, similar to traditional auto-incrementing IDs.

By enabling the snowflakes.testing configuration setting, the standard SnowflakeIdentifierResolver is automatically swapped with a SequentialIdentifierResolver. This generates IDs that are realistic in length and follow a standard auto-incrementing pattern.

When in testing mode, Snowflake IDs can be grouped using the $group argument in the BradieTilley\Snowflakes\SnowflakeGenerator::make()->id($group) or BradieTilley\Snowflake\Snowflake::id($group) methods. The $group is automatically set to the respective model class name.

For example, both Product::create() and User::create() generate an ID of 9000000000000000001, then 9000000000000000002, then 9000000000000000003 and so-on.

Author

cmariesprp/laravel-snowflake 适用场景与选型建议

cmariesprp/laravel-snowflake 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 cmariesprp/laravel-snowflake 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cmariesprp/laravel-snowflake 我们能提供哪些服务?
定制开发 / 二次开发

基于 cmariesprp/laravel-snowflake 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-16