malico/laravel-nanoid 问题修复 & 功能扩展

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

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

malico/laravel-nanoid

Composer 安装命令:

composer require malico/laravel-nanoid

包简介

README 文档

README

Generate Nanoid-based, Stripe-style IDs for your Eloquent models.

Installation

composer require malico/laravel-nanoid

Quick Start

Add HasNanoids to your model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Malico\LaravelNanoid\HasNanoids;

class Book extends Model
{
    use HasFactory;
    use HasNanoids;
}

Use a string primary key in your migration:

public function up(): void
{
    Schema::create('books', function (Blueprint $table) {
        $table->string('id')->primary();
        $table->timestamps();
    });
}

You can also scaffold migrations with:

php artisan make:nanoid-migration

make:nanoid-migration accepts the same arguments as make:migration.

Configuration

You can configure generated IDs per model with these properties (or methods with the same names):

  • nanoidPrefix: static prefix, for example ord_
  • nanoidLength: fixed length (10) or random range ([10, 20])
  • nanoidAlphabet: allowed characters
  • nanoidFormat: structured format like {3}-{4}

Each option supports either:

  • A single value for all unique ID columns
  • A keyed array per column

nanoidPrefix

// One prefix for all generated ids
protected $nanoidPrefix = 'p-';

// Per-column prefixes
protected $nanoidPrefix = ['id' => 'p-', 'username' => 'u_'];

nanoidLength

// One fixed length for all generated ids
protected $nanoidLength = 12;

// Random length between min and max for all generated ids
protected $nanoidLength = [2, 5];

// Per-column fixed length
protected $nanoidLength = ['id' => 7, 'username' => 12];

// Per-column ranged length (min, max)
protected $nanoidLength = ['id' => [2, 5], 'username' => [8, 10]];

// Invalid: range must have exactly two values [min, max]
protected $nanoidLength = ['id' => [2]];

nanoidAlphabet

// One alphabet for all generated ids
protected $nanoidAlphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';

// Per-column alphabets
protected $nanoidAlphabet = [
    'id' => 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789',
    'username' => 'abcdefghijklmnopqrstuvwxyz',
];

nanoidFormat

// One format for all unique id columns
protected $nanoidFormat = 'u_{4}-{4}';

// Per-column formats
protected $nanoidFormat = ['id' => 'p-{7}', 'username' => 'u_{4}-{4}'];

Full model example

<?php

use Illuminate\Database\Eloquent\Model;
use Malico\LaravelNanoid\HasNanoids;

class SessionToken extends Model
{
    use HasNanoids;

    protected $nanoidAlphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
    protected $nanoidPrefix = [
        'id' => 'p-',
    ];
    protected $nanoidLength = 12;
    protected $nanoidFormat = [
        'username' => 'u_{8}',
    ];

    public function uniqueIds(): array
    {
        return ['id', 'username'];
    }
}

// id: p-8F4Z2K9T7Q1M
// username: u_A8K9P2QW

Format Patterns

Use nanoidFormat when you want readable, segmented IDs.

Supported placeholders:

  • {n} generates exactly n random characters
  • {min-max} generates a random length between min and max

All other characters are kept as-is (-, _, ., spaces, and so on).

class TrackingCode extends Model
{
    use HasNanoids;

    protected $nanoidFormat = 'TRK-{3}-{3-4}-{6}';
}

// TRK-X9a-k2Pm-8Qw1Zr
// TRK-L0p-r7A-2bV9tK
class Coupon extends Model
{
    use HasNanoids;

    protected $nanoidAlphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
    protected $nanoidFormat = '{4} {4}';
}

// 7KQ2 M9TZ

Rules:

  • If nanoidFormat is set, it fully controls the generated shape
  • nanoidFormat cannot be combined with nanoidPrefix
  • nanoidFormat cannot be combined with nanoidLength
  • Without nanoidFormat, generation falls back to nanoidPrefix + nanoidLength
  • With per-column arrays, these conflict rules apply per column

Benchmark

Run the included micro-benchmark:

php benchmarks/nanoid.php

Choosing an ID Type

  • Use this package when you want random, non-sequential, human-friendly IDs (for example p-8F4Z2K9T7Q1M)
  • Use Laravel HasUlids when you want sortable IDs that preserve creation order better
  • Use auto-incrementing integers when you need simple sequential IDs and the best insert locality

If your system requires strictly incremental IDs, this package is not the right tool.

Safety Notes

  • Short IDs are easier to collide and easier to guess; increase length for public or sensitive resources
  • NanoID's own guidance compares default NanoID entropy with UUID v4 (similar collision profile)
  • Always choose size/alphabet based on your scale and threat model
  • For custom sizes, check collision estimates with: https://zelark.github.io/nano-id-cc/

If you are upgrading from 0.x, see UPGRADE.MD.

Author

Ndifon Desmond Yong

malico/laravel-nanoid 适用场景与选型建议

malico/laravel-nanoid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 96.45k 次下载、GitHub Stars 达 45, 最近一次更新时间为 2021 年 10 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 45
  • Watchers: 1
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-17