Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TheBlueDragon's avatar

Quiz App, How to pass random questions from php laravel to JS ?

hello,

i have a quiz App project, now iam done with all issues from the dashboard now coming to the point where the student will make the exam so now i have some issues 1 - i need to choose random 30 question out of my database 2 - i need to pass all of them with there answers and the correct answer to a java script as in my js i have this

var quiz = [
    {
        "question"      :   "my question from my db like ( $question->question) ",
        "image"         :   "path/to/image  ( $question->image) ",
        "choices"       :   [
                                "( $question->answers[0]) ",
                                "( $question->answers[1])",
                                "( $question->answers[2])",
                            ],
        "correct"       :   "( still didnt implement a method in my Question model to get the correct answer as my correct answer is a Boolean field in the answers table )",
    },
    etc... to the next question 
    ];

i try to use laracasts utilities but i didnt know how to pass all of these object

is there way to do it with js or it will be better if its passed to Vuejs =( ?

any suggestion or some example or some idea to implement this

thank you


edit for the correct method i implement it in this way

public function correctAnswer()
{
    $answer = $this->answers()->whereCorrect(1)->first();
    return $answer->answer;
}

so the call will be like this 'correct' => $question->correctAnswer() to get the text of the answer it self

0 likes
6 replies
christopher's avatar

I would not store all these informations in your blank js file.

I would make a table with a question and a pivot table with the answers. The question table would also has a column "answer_id".

And if a user submits the answer you call your php method and if the answer id of the submitted form === answer_id of the database column you return a 200 success otherwise you return an error message to your js function.

TheBlueDragon's avatar

i was think to make it like this but it can be done by this method i make

 $answer = $this->answers()->whereCorrect(1)->first(); 

this line it can do the job same as the pivot table

TheBlueDragon's avatar

i fix my method and now the array result like what i want but i didnt find a way to pass it =(

$questions = Questions::all();

    for($i = 0; $i< count($questions); $i++) {
        $all[] = [
            'question' => $questions[$i]->question,
            'image' => $questions[$i]->image,
            'choices' => $questions[$i]->getAnswers($questions[$i]->id),
            'correct' => $questions[$i]->correctAnswer(),
        ];
    }
TheBlueDragon's avatar

try with json_encode method but the result is like this

[{&quot;question&quot;:&quot;First &quot;,&quot;image&quot;:&quot;1448446745.jpg&quot;,&quot;choices&quot;:[&quot;one&quot;,&quot;two answer&quot;,&quot;three&quot;],&quot;correct&quot;:&quot;one&quot;},

i dono why this happen !!

TheBlueDragon's avatar

great i found the issue it was in the blade syntax >> var quiz = {!! $all !!};

now i need to filter all the mess i have and convert the jquery to vuejs if that possible >_<

sebastiangperez's avatar

Sending the correct question to the frontend with all the responses is easily hackable ..

Please or to participate in this conversation.