我在形式上使用了数组名称。这将路由到下面所示的控制器函数。
  <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
发布于 2016-05-12 05:40:27
您不能传递$mycheck变量来查看。
替换这一行:
return View('quiz.check',['quiz'=>$ch,'input'=>$input,'count'=>$count]);     在这方面:
return View('quiz.check',['quiz'=>$ch,'input'=>$input,'count'=>$count,'mycheck'=>$mycheck]);   此外,看起来您试图访问变量$q --根据传递给视图的数据,它实际上是变量$quiz。
https://stackoverflow.com/questions/37176827
复制相似问题