2

I am trying to make Quiz application. I have more than 600 questions in various categories such as jp, sn, ei etc. Now I want to show the questions and options to users from various categories in randomly. But getting error. Would someone help me to do right. I tried something like this - In my Controller.php

public function index()
{
    $jp = DB::table('e_questions')->take(2)->inRandomOrder()->where('dichotomy', '=', 'JP')->get();
    $sn = DB::table('e_questions')->take(2)->inRandomOrder()->where('dichotomy', '=', 'SN')->get();
    $ei = DB::table('e_questions')->take(2)->inRandomOrder()->where('dichotomy', '=', 'EI')->get();
    $tf = DB::table('e_questions')->take(2)->inRandomOrder()->where('dichotomy', '=', 'TF')->get();
    $equestions = array("$jp","$sn","$ei","$tf");
    //dd($equestions);

  return view('question.english.index', compact('equestions'));
}

And in my view index.blade.php

             @foreach($equestions as $equestion)
                 <p>{{ $equestion->question }}</p>
                 <p>{{ $equestion->option1 }}</p>
                 <p>{{ $equestion->option2 }}</p>
             @endforeach
2
  • I removed this. Commented Jan 1, 2018 at 14:33
  • try like this $equestions = array_merge($jp,$sn,$ei,$tf); and please show the error which you facing. Commented Jan 1, 2018 at 14:43

1 Answer 1

1

First of all, create an array like this:

$equestions = [$jp, $sn, $ei, $tf];

Then iterate over the data:

@foreach($equestions as $equestionType)
    @foreach($equestionType as $equestion)
        <p>{{ $equestion->question }}</p>
        <p>{{ $equestion->option1 }}</p>
        <p>{{ $equestion->option2 }}</p>
    @endforeach
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.