tenthfeet/laravel-sequence 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tenthfeet/laravel-sequence

Composer 安装命令:

composer require tenthfeet/laravel-sequence

包简介

A flexible laravel package for generating sequential numbers with various patterns and reset policies.

README 文档

README

A lightweight, flexible Laravel package for generating format-based sequential values with safe DB locking and reset policies.

Features

  • Concurrent-safe sequence generation with DB row locking
  • Custom patterns: {YYYY}, {MM}, {DD}, {H}, {M}, {S}, {SEQ}, {SEQ:N}, {FY}
  • Reset policies: none, yearly, monthly, daily, financial_year
  • Model-scoped sequences (per record)
  • Preview next value without incrementing

Compatibility

  • PHP: 8.2, 8.3, 8.4, and 8.5
  • Laravel: 10.x, 11.x, 12.x, and 13.x

Installation

Install via Composer:

composer require tenthfeet/laravel-sequence

Publish config and migration:

php artisan vendor:publish --provider="Tenthfeet\Sequence\SequenceServiceProvider" --tag="config"
php artisan vendor:publish --provider="Tenthfeet\Sequence\SequenceServiceProvider" --tag="migrations"

Run migrations:

php artisan migrate

Configuration

Published config: config/sequences.php

return [
    'table' => 'sequences',
    'default_pattern' => '{SEQ:3}',
    'default_padding_character' => '0',
    'default_reset_policy' => \Tenthfeet\Sequence\Enums\ResetPolicy::None,
    'financial_year' => [
        'start_month' => \Carbon\Month::April,
    ],
];

Define a sequence

Create a sequence definition:

php artisan make:sequence InvoiceSequence

Example class:

namespace App\Sequences;

use Tenthfeet\Sequence\SequenceDefinition;
use Tenthfeet\Sequence\Enums\ResetPolicy;

final class InvoiceSequence extends SequenceDefinition
{
    public function key(): string
    {
        return 'invoice';
    }

    public function __construct()
    {
        $this->pattern('INV-{YYYY}-{SEQ:4}')
            ->resetPolicy(ResetPolicy::Yearly);
    }
}

Generate sequences

use Tenthfeet\Sequence\Sequence;
use App\Sequences\InvoiceSequence;

$sequence = new InvoiceSequence();
$value = Sequence::using($sequence)->next();
$preview = Sequence::using($sequence)->previewNext();

Model-specific sequences

use App\Sequences\ProjectTaskSequence;
use Tenthfeet\Sequence\Sequence;

$project = App\Models\Project::find(1);
$definition = (new ProjectTaskSequence())->forModel($project);
$value = Sequence::using($definition)->next();

Runtime overrides

$definition = (new InvoiceSequence())
    ->pattern('INV-{YYYY}-{MM}-{SEQ:5}')
    ->padWith('0')
    ->resetPolicy(ResetPolicy::Monthly)
    ->financialYearStartsIn(\Carbon\Month::April)
    ->forModel($project)
    ->usingDate(now());
$value = Sequence::using($definition)->next();

Available fluent methods

  • pattern(string $pattern) - Set the sequence pattern
  • padWith(string $char) - Set the padding character for sequence numbers
  • resetPolicy(ResetPolicy $policy) - Set when the sequence should reset
  • financialYearStartsIn(Month $month) - Set the financial year start month
  • forModel(Model $model) - Scope the sequence to a specific model instance
  • usingDate(Carbon $date) - Override the date used for date-based tokens

Pattern tokens

Token Output example Description
{YYYY} 2026 4-digit year
{YY} 26 2-digit year
{MM} 03 month
{DD} 16 day
{H} 13 hour
{M} 45 minute
{S} 09 second
{SEQ} 1 counter raw
{SEQ:N} 0001 padded counter
{FY} 2025-26 financial year
{FY:YY-YY} 25-26 financial year
{FY:YYYY-YY} 2025-26 financial year
{FY:YYYY-YYYY} 2025-2026 financial year

Reset policies

use Tenthfeet\Sequence\Enums\ResetPolicy;

ResetPolicy::None;
ResetPolicy::Yearly;
ResetPolicy::Monthly;
ResetPolicy::Daily;
ResetPolicy::FinancialYear;

Rollback sequences

use Tenthfeet\Sequence\Sequence;
use App\Sequences\InvoiceSequence;

$sequence = new InvoiceSequence();

// Rollback by 1 step (default)
Sequence::using($sequence)->rollback();

// Rollback by multiple steps
Sequence::using($sequence)->rollback(3);

Notes

Sequence rows are grouped by key, reset_value, and optional model_type / model_id.

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

MIT

tenthfeet/laravel-sequence 适用场景与选型建议

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

它主要适用于以下技术方向: 「generator」 「laravel」 「sequence」 「serial number」 「auto-increment」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 489
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-22