PHPackages                             peibin/laravel-leaf - 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. peibin/laravel-leaf

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

peibin/laravel-leaf
===================

Peibin leaf distributed id component for Laravel.

v1.1.0(2y ago)022MITPHPPHP &gt;=8.0

Since Aug 29Pushed 2y ago1 watchersCompare

[ Source](https://github.com/peibinzhu/laravel-leaf)[ Packagist](https://packagist.org/packages/peibin/laravel-leaf)[ RSS](/packages/peibin-laravel-leaf/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

Laravel leaf 分布式ID组件
====================

[](#laravel-leaf-分布式id组件)

安装
--

[](#安装)

运行下面命令进行安装

```
composer require peibin/laravel-leaf
```

请执行下面sql语句建立leaf数据表

```
CREATE
TABLE `leaf_alloc` (
  `biz_tag` varchar(128) NOT NULL DEFAULT '' COMMENT '业务key',
  `max_id` bigint(20) unsigned NOT NULL DEFAULT '1' COMMENT '当前已经分配了的最大id',
  `step` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '初始步长，也是动态调整的最小步长',
  `description` varchar(255) NOT NULL DEFAULT '' COMMENT '业务key的描述',
  `update_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
  PRIMARY KEY (`biz_tag`),
  UNIQUE KEY `idx_biz_tag` (`biz_tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Leaf分布式id号段分配表';
```

模型需实现 `IDAllocModel` 接口

模型方法 `getLeafAlloc` SQL语句：

```
SELECT `biz_tag`, `max_id`, `step`, `update_time`
FROM `leaf_alloc`
WHERE biz_tag = 'TagName';
```

模型方法 `updateMaxIdAndGetLeafAlloc` SQL语句：

```
UPDATE `leaf_alloc`
SET max_id      = max_id + step,
    update_time = Time
WHERE biz_tag = 'TagName';
```

使用方法
----

[](#使用方法)

```
// 初始化，定义成单例
$this->app->singleton(SegmentIDGenImpl::class, function ($app) {
    $idGen = new SegmentIDGenImpl($app);
    $idGen->setModel(new LeafAllocModel());
    $idGen->init();
    return $idGen;
});

// 调用方法
function segmentId(string $tag): Result
{
    $segment = app()->make(SegmentIDGenImpl::class)->get($tag);
    if ($segment->getStatus() == Result::STATUS_EXCEPTION) {
        throw new \RuntimeException('The generation number segment id is abnormal. CODE:' . $segment->getId());
    }
    return $segment;
}

$result = segmentId('test_tag');
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

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

Total

2

Last Release

1089d ago

### Community

Maintainers

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

---

Top Contributors

[![peibinzhu](https://avatars.githubusercontent.com/u/26355253?v=4)](https://github.com/peibinzhu "peibinzhu (2 commits)")

---

Tags

phplaravelconfig-center

### Embed Badge

![Health badge](/badges/peibin-laravel-leaf/health.svg)

```
[![Health](https://phpackages.com/badges/peibin-laravel-leaf/health.svg)](https://phpackages.com/packages/peibin-laravel-leaf)
```

###  Alternatives

[laragear/preload

Effortlessly make a Preload script for your Laravel application.

119363.5k](/packages/laragear-preload)[napp/xray-laravel

AWS X-Ray for Laravel applications.

61407.3k](/packages/napp-xray-laravel)[tapp/filament-google-autocomplete-field

Filament plugin that provides a Google Autocomplete field

3098.1k](/packages/tapp-filament-google-autocomplete-field)[tzookb/tbmsg

users messaging system

10917.1k](/packages/tzookb-tbmsg)[mindtwo/native-enum

Package for using native php enums.

2626.0k1](/packages/mindtwo-native-enum)[pyaesoneaung/laravel-myanmar-tools

A package for Myanmar tools which extend Laravel’s core.

291.4k](/packages/pyaesoneaung-laravel-myanmar-tools)

PHPackages © 2026

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