0

I want to pass the parameter $questions to view, but it gives the following error:

ErrorException (E_ERROR) Undefined variable: questions (View: C:\Users\Krishan\Documents\GitHub\GroupProject\lcurve\resources\views\quizz\questions\index.blade.php)

This is my controller index function part:

public function index()
{
    $questions = Question::all();    
    return view('quizz/questions.index', compact('questions'));
}

This is a part Of my view:

<tbody>
    @if (count($questions_options) > 0)
        @foreach ($questions_options as $questions_option)
            <tr data-entry-id="{{ $questions_option->id }}">
                <td></td>
                <td>{{ $questions_option->question->question_text or '' }}</td>
                <td>{{ $questions_option->option }}</td>
                <td>{{ $questions_option->correct == 1 ? 'Yes' : 'No' }}</td>
                <td>
                    <a href="{{ route('quizz/questions_options.show',[$questions_option->id]) }}" class="btn btn-xs btn-primary">View</a>-->
                    <!--<a href="{{ route('questions_options.edit',[$questions_option->id]) }}" class="btn btn-xs btn-info">Edit</a>-->
                    {!! Form::open(array(
                                        'style' => 'display: inline-block;',
                                        'method' => 'DELETE',
                                        'onsubmit' => "return confirm('".trans("quickadmin.are_you_sure")."');",
                                        'route' => ['questions_options.destroy', $questions_option->id])) !!}
                                    {!! Form::submit(trans('quickadmin.delete'), array('class' => 'btn btn-xs btn-danger')) !!}
                                    {!! Form::close() !!} 
                </td>
            </tr>
        @endforeach
    @else
        <tr>
            <td colspan="5">no_entries_in_table</td>
        </tr>
    @endif
</tbody>

enter image description here

2 Answers 2

1

where is questions_options coming from? You are passing questions. So your for loop should be

@if (count($questions) > 0)
  @foreach ($questions as $question)
     //rest of your code
  @endforeach
@endif

and your return view part can be return view(quizz.questions.index, compact('questions'))

Sign up to request clarification or add additional context in comments.

Comments

1

Firstly, the error message you have mention should be shown. The error message should be: Undefined variable: questions_options (View:C:\Users\Krishan\Do........

Because you are passing questions to view but you are accessing question_options in view. So, it should say question_options in undefined in view.

Besides, do you know you can avoid this count check? You can use laravel's forelse tag here a below:

@forelse($questions as $question)
     //Your table goes here
@empty
   <tr>
      <td colspan="5">no_entries_in_table</td>
   </tr>
@endforelse

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.