dimanche 18 mars 2018

How to get the values of related tables in Laravel?

These are the structures of the tables I am using

Schema::create('questions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('question_text');
        $table->integer('question_bank_id')->unsigned();
        $table->foreign('question_bank_id')->references('id')->on('question_banks');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });
}


Schema::create('answers', function (Blueprint $table) {
            $table->increments('id');
            $table->text('answer_text');
            $table->boolean('correct_answer');
            $table->integer('question_id')->unsigned();
            $table->foreign('question_id')->references('id')->on('questions');
            $table->timestamps();
        });

For each question there are more 2 answer(including correct and incorrect answers) I am wondering what is the best practice to pull the questions and their answers from the tables?

For example, if I want to list all of the questions with the question_bank_id = 1 like the following

Q. What is the milky way ? 1) a Galaxy 2) a planet 3) yada yada

Q. Where are you from? 1) Earth 2) Mars 3) Gaga land

.......



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2HJSgfY
via IFTTT

Aucun commentaire:

Enregistrer un commentaire