25日的份 将laravel 默认的id 改为 uuid 的方法,以及passport 使用uuid
user 部分 当不使用传统id时,
先在模型中写入 关闭默认排序
1 public $incrementing = false ;
再migrate中修改
1 $table ->increments ('id' );
为
1 $table ->uuid ('id' )->primary ();
随后运行la5 migrate
新建App\Traits\Uuid
内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?php namespace App \Traits ;use Illuminate \Support \Str ;trait Uuid { protected static function boot ( ) { parent ::boot (); static ::creating (function ($model ) { $model ->{$model ->getKeyName ()} = (string ) Str ::orderedUuid (); }); } }
随后在模型中添加
1 2 3 use App \Traits \Uuid ;use Uuid ;
之后使用laravel自带的auth可以正常注册了。密码找回功能也可以使用。
参考文档
passport 部分 当 passport安装完成后
执行php artisan vendor:publish --tag=passport-migrations
复制迁移到migrate目录
修改CreateOauthClientsTable
1 2 3 4 5 6 $table ->increments ('id' );$table ->integer ('user_id' )->index ()->nullable ();$table ->uuid ('id' )->primary ();$table ->uuid ('user_id' )->index ()->nullable ();
其他迁移将client_id与user_id 改为使用uuid
App\Providers\AppServiceProvider中引入部分添加
1 2 use Laravel \Passport \Client ;use Illuminate \Support \Str ;
boot()部分添加
1 2 3 4 5 6 7 Client ::creating (function (Client $client ) { $client ->incrementing = false ; $client ->id = (string ) Str ::orderedUuid (); }); Client ::retrieved (function (Client $client ) { $client ->incrementing = false ; });
register() 部分添加
1 Passport ::ignoreMigrations ();
其他部分按照文档即可正常使用。
参考文档
user 路径修改 在App\Http\Controllers\Auth\RegisterController中修改
在config中的providers修改到对应路径 之后可以正常使用
laravel 包开发 包的开发非常有利于后期的维护,同时也可以有效的分配工作任务,对未来的团队工作有极大的优势。
主要是功能混在一起,找起来超级烦。
有必要将独立的功能分散出去。
参考地址 1 参考地址 2
需要说明的是 namespace与autoload超级重要
2019年10月11日 更新 Ramsey\Uuid\Exception\UnsatisfiedDependencyException 错误。
1 composer require "moontoast/math"