【PHP】多対多


参考ページ

Laravel 8.x

構成と概要

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

Questionsテーブル

  • model
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
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
class Category extends Model
{
    use HasFactory;

    protected $fillable = [
        'body',
    ];

    public function questions()
    {
        return $this->belongsToMany('App\Question');
    }
}
  • Migration File
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
class Category_question extends Model
{
    use HasFactory;

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

}
  • Migration File
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');
        });
    }