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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法