正在检索数组名称


Retrieving an array name

我在表单中使用了数组名称。这将路由到下面显示的控制器函数。

  <form method="POST" action="{{url('quiz/check')}}"
{!! csrf_field() !!} 
@foreach ($quiz as $q)
@if($q->level=='1')
{{ $q->qid }}.  
{{ $q->question }}<br>
<input type='radio' name='mycheck[{{$q->qid}}]' value='1'>     
{{ $q->opt1 }}<br>
<input type='radio' name='mycheck[{{$q->qid}}]' value='2'>     
{{ $q->opt2 }}<br>
<input type='radio' name='mycheck[{{$q->qid}}]' value='3'>    
{{ $q->opt3 }}<br>
<input type='radio' name='mycheck[{{$q->qid}}]' value='4'>   
{{ $q->opt4 }}<br><br>
@endif
@endforeach        
    <button type="submit" class="btn btn-default">Get result</button>
                            </form>

我正试图在控制器中检索为:

public function check(Request $request)
  {
    $count=0; 
    $input=$request->all();
    $mycheck=$input['mycheck'];
    $ch = DB::select('select * from quiz where category="gk" ');
    return View('quiz.check',['quiz'=>$ch,'input'=>$input,'count'=>$count]);     
 }

查看文件check.blade.php

Results:
@foreach ($quiz as $q)
   @if(array_key_exists($q->qid, $mycheck) && $mycheck[$q->qid]==$q->answer)
      {{$count=$count+1}}
@endif
@endforeach        
You scored {{$count}}.

现在,我得到这个错误:

未定义变量:视图文件中的mycheck

您没有将$mycheck变量传递给视图。

替换此行:

return View('quiz.check',['quiz'=>$ch,'input'=>$input,'count'=>$count]);     

有了这个:

return View('quiz.check',['quiz'=>$ch,'input'=>$input,'count'=>$count,'mycheck'=>$mycheck]);   

此外,根据传递给视图的Your数据,看起来像您试图访问变量$q,实际上是变量$quiz