dongqi4085 2014-11-21 23:02 采纳率: 0%
浏览 45
已采纳

无法将数组从控制器传递到laravel中的视图

i am new to laravel and i have been following the documentation and several videos but for hours now i have been trying to pass an array from the controller to a view but i keep getting this error in the view:

Undefined variable: quiz

This is the controller code:

public function SetQuestions($id)
{
    $query = Quiz::find($id);

    $quiz = array(
        'id' => $query->id,
        'noQuestions' => $query->no_questions,
        'totalQuizScore' => $query->total_quiz_score
    );
    return View::make('quiz.set-questions')->with($quiz);
}

This is the route:

Route::resource("/quiz/set-questions/{id}", 'QuizController@SetQuestions');

This is the code in the view:

 <?php var_dump($quiz); ?>

For now i'm just dumping the data to see if the value changes form null.

  • 写回答

3条回答 默认 最新

  • douhe1864 2014-11-21 23:07
    关注

    As described in the docs the view method takes two arguments. The first is the name of the variable you want it to be accessible by in the view, the second is the actual data.

    return View::make('quiz.set-questions')->with('quiz', $quiz);
    

    There are also a few other options

    return View::make('quiz.set-questions')->withQuiz($quiz);
    
    return View::make('quiz.set-questions', array('quiz'=>$quiz));
    
    return View::make('quiz.set-questions', compact($quiz));
    

    All these have the same result

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?