alecgarcia / laravel-uid
创建类似于Stripe生成的UID。这些UID可以用于您的模型或单独使用。
v1.0.0
2022-01-30 02:58 UTC
Requires (Dev)
- illuminate/support: ~7|~8
- orchestra/testbench: ^6.24
- phpunit/phpunit: ~9.0
README
此包创建与Stripe用于模型或单独使用的UID类似的UID。
安装
通过Composer
$ composer require alecgarcia/laravel-uid
使用
与模型一起使用
1. 在表中添加uid列
使用现有模型
- 添加迁移
php artisan make:migration --table users add-uid-to-users
- 打开您刚刚创建的迁移文件并添加一个
uid
字段。- 如果您在默认配置或模型中覆盖了列名,请确保在您的表中创建匹配的列。
/** * 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'); }); }
使用新模型
- 打开模型的迁移文件并添加一个
uid
字段- 如果您在默认配置或模型中覆盖了列名,请确保在您的表中创建匹配的列。
/** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('uid', 32)->unique(); $table->timestamps(); }); }
2. 将特质添加到您的模型中
<?php namespace App\Models; use Alecgarcia\LaravelUid\Traits\Uid; use Illuminate\Database\Eloquent\Model; class User extends Model { use UID; }
静态使用
<?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
定制
使用特质
您可以将以下属性添加到模型中以配置Uid特质。
- 这将覆盖默认值以及配置文件。
<?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 }
使用配置文件设置默认值
- 发布配置文件。
$ php artisan vendor:publish --tag laravel-uid.config
- 在
app/config/laravel-uid.php
文件中自定义默认值。
<?php return [ /* |-------------------------------------------------------------------------- | Customize how the uid is created and used |-------------------------------------------------------------------------- */ 'characters' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'prefix_separator' => '_', 'uid_column' => 'uid', 'length' => 16, 'check' => true, ];
变更日志
请参阅changelog以获取有关最近更改的更多信息。
测试
$ php vendor/bin/phpunit
贡献
请参阅contributing.md以获取详细信息和工作列表。
安全
如果您发现任何安全相关的问题,请通过电子邮件hello@alecgarcia.com联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT。请参阅许可证文件以获取更多信息。