Laravel 6 - UUID as Primary Key for Passport API
チュートリアルを続けたくない場合は、このチュートリアルと同じ機能を備えたhttps://github.com/JamesHemery/laravel-uuidをいつでも使用できます
If you don´t want to follow the tutorial, you can always use https://github.com/JamesHemery/laravel-uuid which has the same functionality as this tutorial
laravelをインストールする
Install laravel
ユーザーテーブルの移行の変更
modify users table migration
$table->uuid('id')->primary();
パスポートをインストールする
Install Passport
移行を実行する
run migrations
composer laravel / dbalをインストールします
composer Install laravel/dbal
テーブルを変更する新しい移行を作成する
create a new migration to change table
Schema::table("oauth_access_token",function (Blueprint $table){
$table->string("user_id")->change();
});
移行を再度実行します
run migrations again
ユーザーモデル内に、以下を追加します
Inside the User Model, add the following
use Illuminate\Support\Str;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
public $incrementing = false; //No more autoincrement field
protected static function boot()
{
parent::boot(); // TODO: Change the autogenerated stub
//Generate a new UUID when creating a new registry
self::creating(function ($model){
$model->id = (string) Str::uuid();
});
}
....
}
この部分を他のモデルで再利用可能にしたい場合は、特性を作成できます
If you want to make this part reusable for other models, you can create a Trait
app \ Traits \ HasUuid.php
にファイルを作成します
create a file in app\Traits\HasUuid.php
<?php
namespace App\Traits;
use Illuminate\Support\Str;
trait HasUuid{
public function getIncrementing()
{
return false;
}
protected static function boot()
{
parent::boot(); // TODO: Change the autogenerated stub
self::creating(function ($model){
$model->id = (string) Str::uuid();
});
}
}
モデルでは、「HasApiToken」の横に新しい特性「HasUuid」を追加できます
In the model, next to HasApiToken
you can add the new trait HasUuid
class User extends Authenticatable
{
use HasApiTokens, Notifiable, SoftDeletes, HasUuid;
....
残りは、APIの作成方法に関する以前のチュートリアルに従うことができます
The rest you can follow my previous tutorials on how to create an API