alecgarcia/laravel-uid
Composer 安装命令:
composer require alecgarcia/laravel-uid
包简介
Create UIDs like the ones Stripe generates. These can be used on your models or on their own.
关键字:
README 文档
README
This package creates UIDs like the ones Stripe uses for your models or on their own.
Installation
Via Composer
$ composer require alecgarcia/laravel-uid
Usage
To use with a model
1. Add uid column to table
With an existing Model
- Add a migration
php artisan make:migration --table users add-uid-to-users
- Open up the migration file you just created and add a
uidfield.- If you overrode the column name in either the config for the default or in your model, make sure you create the column in your table to match.
/** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { $table->string('uid', 32)->after('id')->unique(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('uid'); }); }
With a new Model
- Open up the migration file for the model and add a
uidfield- If you overrode the column name in either the config for the default or in your model, make sure you create the column in your table to match.
/** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('uid', 32)->unique(); $table->timestamps(); }); }
2. Add the trait to your model
<?php namespace App\Models; use Alecgarcia\LaravelUid\Traits\Uid; use Illuminate\Database\Eloquent\Model; class User extends Model { use UID; }
To used statically
<?php use Alecgarcia\LaravelUid\Uid; $generatedUid = Uid::make(); // Generated Uid -> "1mVQuIwVrRS9ijwx" // Defaults to no prefix and 16 Characters long $generatedUid = Uid::make('uid', 12); // Generated Uid -> "uid_nuv8V6GH" // Can set the prefix that is used and the length
Customization
Using the trait
You can add the following properties to your model to configure the Uid trait.
- This will override the defaults as well as the config file.
<?php namespace App\Models; use Alecgarcia\LaravelUid\Traits\Uid; use Illuminate\Database\Eloquent\Model; class User extends Model { use UID; public static string $uidPrefix = 'usr'; // Defaults to models class name public static int $uidLength = 10; // Defaults to 16 total characters public static bool $uidCheck = false; // Default is true public static string $uidColumn = 'uidCustomColumn'; // Default is uid }
Using the config file for defaults
- Publish the config file.
$ php artisan vendor:publish --tag laravel-uid.config
- Customize the defaults in the
app/config/laravel-uid.phpfile.
<?php return [ /* |-------------------------------------------------------------------------- | Customize how the uid is created and used |-------------------------------------------------------------------------- */ 'characters' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'prefix_separator' => '_', 'uid_column' => 'uid', 'length' => 16, 'check' => true, ];
Change log
Please see the changelog for more information on what has changed recently.
Testing
$ php vendor/bin/phpunit
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email hello@alecgarcia.com instead of using the issue tracker.
Credits
- Alec Garcia
- All Contributors
- Originally influenced by dpods/laravel-uid
License
MIT. Please see the license file for more information.
alecgarcia/laravel-uid 适用场景与选型建议
alecgarcia/laravel-uid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「UID」 「LaravelUid」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alecgarcia/laravel-uid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alecgarcia/laravel-uid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alecgarcia/laravel-uid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP library for generating and working with identifiers, including UUIDs, ULIDs, and Snowflakes
Token manager Laravel
Small Unique Identifier - Quite like an ULID, but half smaller (64 bits).
Provides Doctrine types for mediagone/small-uid package.
SilverStripe module for adding an unique id (UUID) to a DataObject
Alfabank REST API integration
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-24