简体   繁体   English

Laravel参数从控制器到视图的传递错误

[英]Laravel Parameter passing error from controller to view

I want to pass the parameter $questions to view, but it gives the following error: 我想传递参数$ questions进行查看,但是出现以下错误:

ErrorException (E_ERROR) Undefined variable: questions (View: C:\\Users\\Krishan\\Documents\\GitHub\\GroupProject\\lcurve\\resources\\views\\quizz\\questions\\index.blade.php) ErrorException(E_ERROR)未定义的变量:问题(视图: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 在此处输入图片说明

where is questions_options coming from? 其中questions_options来自哪里? You are passing questions . 您正在传递questions So your for loop should be 所以你的for循环应该是

@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')) 您的返回视图部分可以是return view(quizz.questions.index, compact('questions'))

Firstly, the error message you have mention should be shown. 首先,应该显示您提到的错误消息。 The error message should be: Undefined variable: questions_options (View:C:\\Users\\Krishan\\Do........ 错误消息应该是: Undefined variable: questions_options (View:C:\\Users\\Krishan\\Do........

Because you are passing questions to view but you are accessing question_options in view. 因为您正在传递要查看的questions ,但是正在访问视图中的question_options So, it should say question_options in undefined in view. 因此,它应在视图未定义状态下说出question_options

Besides, do you know you can avoid this count check? 此外,您知道您可以避免此计数检查吗? You can use laravel's forelse tag here a below: 您可以在以下位置使用laravel的forelse标签:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM