laragoon / uuid
Laragoon Uuid 包
1.0.0
2021-01-10 04:39 UTC
README
Laravel 模型使用的 Uuid 特性
安装
composer require laragoon/uuid
使用
将 uuid
字段添加到数据库表中
$table->uuid('uuid')->unique()->index();
然后在模型中使用 Laragoon\Uuid\Traits\HasUuid
特性
//app/Models/User.php namespace App\Models; use Laragoon\Uuid\Traits\HasUuid; class User extends Authenticatable { use HasUuid; }
就这些!现在当你创建记录时,将生成 Str::uuid()
并自动填充数据库字段
User::create([ 'name' => 'John', 'email' => 'john@doe.com', 'password' => Hash::make('secret') ]);
现在你可以在模型实例上使用 uuid
字段
$user = User::where('name' => 'John')->get(); $user->uuid; // 4d2d3be9-194a-45ad-8157-7b5c01c35c39