无法从控制器传递数组以在Laravel中查看 [英] unable to pass array from controller to view in laravel

查看:84
本文介绍了无法从控制器传递数组以在Laravel中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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

这是控制器代码:

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);
}

这是路线:

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

这是视图中的代码:

 <?php var_dump($quiz); ?>

现在我只是转储数据以查看值是否从null更改.

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

推荐答案

docs view方法带有两个参数.第一个是希望在视图中访问它的变量的名称,第二个是实际数据.

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

还有其他一些选择

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

这篇关于无法从控制器传递数组以在Laravel中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆