vendredi 23 août 2019

Laravel Eloquent Relationshipss

At first, I have a user and quiz tables which has one to one relationship. And then an answers table where user's answer to each quiz's questions are stored. And then that's it, no problem.

But now I wanted to have the user have the ability to take a quiz multiple times, and so I changed the user and quiz relationship to many to many, my question is, is it a good practice to store the quiz_user_id on the quiz_user pivot table on the answers table to represent attempts of the user taking the quiz.

If it is not, what's the better approach?

Schema::create('answers', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger("user_id");
  $table->unsignedBigInteger("quiz_id");
  $table->unsignedBigInteger("question_id");
  $table->enum("student_answer", ['a', 'b', 'c', 'd']);
  $table->softDeletes();
  $table->timestamps();
});

Schema::create('quiz_user', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger('quiz_id');
  $table->unsignedBigInteger('user_id');
  $table->softDeletes();
  $table->timestamps();
});

Schema::create('users', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->string('name');
  $table->string('email')->unique();
  $table->timestamp('email_verified_at')->nullable();
  $table->string('password');
});

Schema::create('quizzes', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->string('title');
  $table->longText('description')->nullable();
  $table->softDeletes();
  $table->timestamps();
});




via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire