devzero2in/laravel-uid-generator 问题修复 & 功能扩展

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

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

devzero2in/laravel-uid-generator

Composer 安装命令:

composer require devzero2in/laravel-uid-generator

包简介

Laravel Unique ID Gegerator

README 文档

README

Latest Version on Packagist Total Downloads License

A flexible and reliable package for generating unique IDs in Laravel. This package allows developers to generate custom IDs for any database table with options for setting prefixes, custom lengths, and field names.

📦 Features

  • ✅ Generate unique IDs for any Eloquent model.
  • ✅ Customize ID length and prefix.
  • ✅ Prevent duplicate ID generation.
  • ✅ Easy-to-use facade for quick integration.
  • ✅ Configurable through Laravel's config system.

🚀 Installation

Install the package via Composer:

composer require devzero2in/laravel-uid-generator

🔧 Configuration

After installation, publish the configuration file (optional):

php artisan vendor:publish --tag=config

This will create a config/uniqueid.php file where you can set default values:

return [
    'default_length' => 8,   // Default ID length
    'default_prefix' => 'ID-', // Default ID prefix
];

⚙️ Usage

Basic Example

Generate a unique ID with default configuration:

use UniqueIdGenerator;

$id = UniqueIdGenerator::generate(App\Models\User::class);

Custom Length and Prefix

Generate a unique ID with custom length and prefix:

$id = UniqueIdGenerator::generate(App\Models\User::class, 'custom_id', 10, 'USR-');

Saving to a Model

Assign and save the generated ID to a model:

use App\Models\User;
use UniqueIdGenerator;

$user = new User();
$user->custom_id = UniqueIdGenerator::generate(User::class, 'custom_id', 12, 'USR-');
$user->name = 'Jane Doe';
$user->email = 'jane@example.com';
$user->save();

🏗️ Advanced Usage

Override the default field name for checking ID uniqueness:

$id = UniqueIdGenerator::generate(App\Models\Order::class, 'order_number', 6, 'ORD-');

🏛️ Service Provider & Facade Registration

If auto-discovery is disabled, manually register the service provider and alias in config/app.php:

'providers' => [
    YourVendor\UniqueIdGenerator\UniqueIdGeneratorServiceProvider::class,
],

'aliases' => [
    'UniqueIdGenerator' => YourVendor\UniqueIdGenerator\Facades\UniqueIdGenerator::class,
],

✅ Testing

Run package tests using PHPUnit:

vendor/bin/phpunit

📜 Changelog

You can view the changelog for more information on recent changes.

🤝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Commit your changes (git commit -m 'Add new feature').
  4. Push to the branch (git push origin feature/YourFeature).
  5. Create a new Pull Request.

🔒 License

This package is open-sourced software licensed under the MIT license.

📩 Support

// A cup of coffee would be appreciated.

Buy Me Coffee

If you need any help or have questions, feel free to open an issue or contact me.

GitHub Issues GitHub PRs

统计信息

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

GitHub 信息

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

其他信息

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