【PHP】多対多

# 参考ページ
[Laravel 8.x](https://readouble.com/laravel/8.x/ja/migrations.html?header=%25E5%25A4%2596%25E9%2583%25A8%25E3%2582%25AD%25E3%2583%25BC%25E5%2588%25B6%25E7%25B4%2584)


# 構成と概要
- CategoryとQuestionテーブルのリレーション
- Code
    - belongsToMany('リレーション先Modelのパス')
    - unsignedBigInteger
    - $table->foreign('カラム名')->references('id')->on('テーブル名')->onDelete('cascade');


# Questionsテーブル
- model

```php
class Question extends Model
{
    use HasFactory;

    protected $fillable = [
        'body',
        'choice_1',
        'choice_2',
        'choice_3',
        'choice_4',
        'answer_body',
        'answer_choice',
        'status_num',
        'user_id',
    ];

    public function workbooks()
    {
      return $this->belongsToMany('App\Workbook');
    }

    public function categories()
    {
      return $this->belongsToMany('App\Category');
    }

    public function users()
    {
        return $this->belongsTO('App\User');
    }
}
```

- Migration File

```php
class CreateQuestionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('questions', function (Blueprint $table) {
            $table->id();
            $table->string('body',500);
            $table->string('choice_1',100);
            $table->string('choice_2',100);
            $table->string('choice_3',100);
            $table->string('choice_4',100);
            $table->string('answer_body');
            $table->integer('answer_choice');
            $table->integer('status_num');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('questions');
    }
}
```

# Categoriesテーブル
- model

```php
class Category extends Model
{
    use HasFactory;

    protected $fillable = [
        'body',
    ];

    public function questions()
    {
        return $this->belongsToMany('App\Question');
    }
}
```

- Migration File

```php
class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('body',30);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}
```



# 中間テーブル
- Model

```php
class Category_question extends Model
{
    use HasFactory;

    protected $fillable = [
        'workbook_id',
        'question_id',
    ];

}
```


- Migration File

```php
public function up()
    {
        Schema::create('category_questions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('category_id');
            $table->unsignedBigInteger('question_id');
            $table->timestamps();

            $table->foreign('category_id')
                ->references('id')
                ->on('categories')
                ->onDelete('cascade');
            $table->foreign('question_id')
                ->references('id')
                ->on('questions')
                ->onDelete('cascade');
        });
    }
```