0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pivotとrequest

Posted at

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',
    ];
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?