pelmered/laravel-ulid 问题修复 & 功能扩展

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

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

pelmered/laravel-ulid

Composer 安装命令:

composer require pelmered/laravel-ulid

包简介

Universally Unique Lexicographically Sortable Identifier(ULID) for Laravel

README 文档

README

This package improves the ULID support in Laravel with the following features:

  • Configurable prefix
  • Configurable length (both time and random parts)
  • Configurable formatting/case
  • Configurable time source
  • Configurable randomness source
  • Provides a Facade for working with ULIDs

ULIDs - What and why

ULIDs are a compact, time-ordered, globally unique identifier that are used to identify resources in a way that is both human-readable and machine-readable.

For a more in-depth technical explanation, see the ULID specification

One of my personal favorite things about ULIDs is that you can double-click on them to select and copy. Try to select and copy these IDs: u_01ARZ3NDEKTSV4RRFFQ69G5FAVY and 4f839b45-fcca-495b-b9ed-0e365270b1c3. The prefixes also makes it immediately clear what kind of ID it is when you see it.

This is great when you are passing IDs around to colleagues or coworkers when debugging or working with support. The hyphens also look pretty ugly and do not have any real meaning for a human.

Latest Stable Version Total Downloads Monthly Downloads License

Tests OtterWise Coverage OtterWise Type Coverage

Tested on PHP 8.4 Tested on OS:es Linux, MacOS, Windows

TODO

  • Add more tests
  • Add docs
  • Add license
  • Add badges
  • Implement custom formatter support

Roadmap

  • Migration tool for migrating an existing database with numeric or UUIDs to ULIDs. Probably as a separate suggested package.

Installation

composer require pelmered/laravel-ulid

Setup

Add the interface Ulidable and the trait HasUlid to your model.

use Pelmered\LaravelUlid\LaravelUlidServiceProvider;
class Post extends Model implements Ulidable
{
    use HasUlid;

    //...
}

Configuration

You can configure the ULID prefix, time length and random length in the model.

use Pelmered\LaravelUlid\LaravelUlidServiceProvider;use Pelmered\LaravelUlid\ValueObject\Ulid;
class Post extends Model implements Ulidable
{
    use HasUlid;

    protected string $ulidPrefix = 'p_';
    protected int $ulidTimeLength = 10;
    protected int $ulidRandomLength = 16;
    protected string $ulidOptions = Ulid::OPTION_UPPERCASE;

    //...
}

You probably shouldn't touch the time length unless you know what you are doing.
The Random part could be optimized based on your needs. A low traffic application could probably use a shorter random part to optimize storage space and performance.

Usage (Work in progress)

Migrations

To create a ULID column in a migration, you can use the ulid or modelULid methods like this to get the correct length.

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Create a table with an ULID primary key like this:
        Schema::create('users', function (Blueprint $table) {
            $table->uLid('id', (new \Workbench\App\Models\User())->getUlidLength())->primary();
        });
        // or like this:
        Schema::create('posts', function (Blueprint $table) {
            $table->modelULid('id', User::class)->primary();
        });
    }
}

Facade

ULID::make(); // '1234567890123456789' - Normal ULID
ULID::make('p_', Carbon::parse('2010-11-12 01:02:03')); // 'u_1234567890123456789' - ULID with prefix 
ULID::make('p_', 10, 10 ); // 'u_1234567890123456789' - ULID with prefix
ULID::fromModel($model);
ULID::isValidUlid('u_1234567890123456789', $model);

Formatting


Config

Publish the config file config/ulid.php to your project.

php artisan vendor:publish --tag=ulid-config

Running tests

Run the test with vendor/bin/pest or composer test.

The test suite has some tests that require a MySQL database. To run these tests, you need to create a .env file in the root of the project with the following contents, adjusted for your setup:

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_ulid_testing
DB_TESTING_DATABASE=laravel_ulid_testing
DB_USERNAME=root
DB_PASSWORD=

You can also skip the MySQL tests the tests by running vendor/bin/pest --exclude-group=mysql.

pelmered/laravel-ulid 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-22