pivot
中間テーブルのことのようで、多対多のリレーションが作れる
Aさん、Bさん、Cさんが居て、営業、エンジニア、人事みたいなのがあって、Aさん、Cさんが営業でBさんがエンジニア見たいのを管理できるみたいな。
↓AIが出したコード例
マイグレーション
// users テーブル
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
// roles テーブル
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
// role_user(中間テーブル)
Schema::create('role_user', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->foreignId('role_id')->constrained()->onDelete('cascade');
$table->timestamps();
});
Requestクラス
これはruleメソッドでバリデーションルールを設定したり、authenticateメソッドでバリデーションに入る前にそのリクエストを受け付けていいのかを判断できる。
public function authorize(): bool
{
return true;←trueに変えないとリクエストが受付されない。
}
public function rules(): array
{
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
}