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条)

报告相同问题?

悬赏问题

  • ¥15 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线