承接 firevel/model-random-id 相关项目开发

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

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

firevel/model-random-id

最新稳定版本:0.0.3

Composer 安装命令:

composer require firevel/model-random-id

包简介

Model random ID generator for Laravel.

README 文档

README

A package for generating random BIGINT IDs as primary keys in Laravel models. These IDs are compatible with MySQL BIGINT columns and JavaScript's MAX_SAFE_INTEGER.

Purpose

Systems using distributed databases like Cloud Spanner or Firestore shouldn't use incremental id's to avoid bottlenecks. UUID can be used as an alternative but long strings can cause performance issues and UX issues. Random BIGINT is a middle ground between incremental id and UUID.

Installation

Install the package via Composer:

composer require firevel/model-random-id

Usage

Database Setup

Ensure your database table uses a BIGINT primary key. For example:

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

Model Setup

Add the trait to your model:

use \Firevel\ModelRandomId\HasRandomId;

Disable auto-incrementing for the primary key:

/**  * Indicates if the IDs are auto-incrementing.  *  * @var bool  */ public $incrementing = false;

Limitations

While random number generation reduces the risk of ID collisions, it is not entirely immune to conflicts. The more IDs you generate, the higher the chance of a collision due to the Birthday Paradox—where random sampling within a finite range (in this case, BIGINT) increases the likelihood of overlap as the number of samples grows. For use cases involving millions of rows or extremely high throughput, consider pre-generating a list of unique IDs and distributing them to avoid runtime conflicts. This approach ensures scalability while preserving randomness.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04