只在一列工作的地方

时间:2016-05-15 15:25:54

标签: php laravel laravel-5

我有这条路线:

Route::get('/quiz/category/{name}', 'playquiz@category');

和这个控制器

public function category($name)
  {
        $ch = quiz::all()->where('category',$name);
        $cat = quiz::all()->where('category',$name)->first();

        return View('quiz.index',['quiz'=>$ch,'cat'=>$cat]);     

 }

索引视图包含一个表单:

<form method="POST" action="{{url("quiz/check/{$cat->category}/3")}}">

重定向到此路线:

Route::post('/quiz/check/{name}/{no}', 'playquiz@check');

带控制器:

  public function check(Request $request, $name, $no)
  {

    $count=0; 
    $input=$request->all();
    $mycheck=$input['mycheck'];
        $eman=$input['name']
        $stmt = quiz::all()->where('category',$name);
        $cat = quiz::all()->where('category',$name)->first();

    foreach ($stmt as $c)
    {

         if(array_key_exists($c->qid, $mycheck) && $mycheck[$c->qid]==$c->answer)
            {
                $count=$count+1;
        }
    }

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

 }

如果我使用category列以外的任何内容,则会出现错误。例如$stmt = quiz::all()->where('level',$no);$stmt = quiz::all()->where('qid','2');$stmt = quiz::all()->where('category',$name)->where('level','3');

重定向到查看check.blade.php

You chose <mark>{{$cat->category}}</mark>  
Hello {{$eman}}<br>.
Results:

    You scored {{$count}}.
         <br>
    ANSWERS LIST:
    <table border='1' class='table table-hover table-striped'>
    <thead style='background-color:silver'><tr><td>id</td><td>Question</td><td>opt 1</td><td>opt 

    2</td><td>opt 3</td><td>opt 4</td><td>Correct answer</td><td>You selected</td></tr></thead>
    @foreach ($stmt as $q)
    <tr><td>{{$q->qid}}</td><td>{{$q->question}}</td><td>{{$q->opt1}}</td><td>{{$q->opt2}}</td>
    <td>{{$q->opt3}}</td><td>{{$q->opt4}}</td><td>{{$q->answer}}</td>

    <td> @if(array_key_exists($q->qid, $mycheck))

            opt {{$mycheck[$q->qid]}}
        @else 
        You have not selected an answer.

    @endif
    </td></tr>
    @endforeach

1 个答案:

答案 0 :(得分:0)

quiz::all()将执行您的查询并返回表中所有行的集合,因此您添加的所有后续->where()实际上都在搜索返回的集合,而不是查询数据库,< / p>

尝试quiz::where('category',$name)->get();

quiz::where('qid','2')->get;