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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题