如何将其他模型中的数据添加到Laravel中的视图中?

如何将其他模型中的数据添加到Laravel中的视图中?,laravel,Laravel,情景: 用户在上一个查看页面中生成问题。例如:“问题1”。这个输入的问题需要出现在未来的创建页面上,以便受访者可以看到它。答案就在后面 我不知道如何把这联系起来。如何从“问题”表中获取数据以显示在“受访者”创建页面上 我的目标是在回答者和问题类之间建立关系,编辑控制器和查看/创建页面 受访者模型: 公共职能问题() { 返回此->hasMany('App\questions'); } 问题模式: public function responses() { 返回此->hasMany('App\r

情景: 用户在上一个查看页面中生成问题。例如:“问题1”。这个输入的问题需要出现在未来的创建页面上,以便受访者可以看到它。答案就在后面

我不知道如何把这联系起来。如何从“问题”表中获取数据以显示在“受访者”创建页面上

我的目标是在回答者和问题类之间建立关系,编辑控制器和查看/创建页面

受访者模型:

公共职能问题()
{
返回此->hasMany('App\questions');
}
问题模式:

public function responses()
{
返回此->hasMany('App\responder');
}
问题控制员:

公共功能索引()
{
$QUOTE=QUOTES::all();
$INQUOTE=问卷::全部();
返回视图('question.index',compact('question',$question',inventory',$inventory));
}
响应控制器:

公共功能索引()
{
$QUOTE=QUOTES::all();
$答辩人=答辩人::全部();
返回视图('responder.index',compact('question',$question','responder',$responder');
}
响应者创建页面:


@foreach($问题作为$插入)
{{$insert->question1}
同意
不同意
@endforeach
目前,我得到一个错误:“未定义变量:问题”

事后看来,问题应该出现在两个单选按钮上方


提前感谢您的帮助

应该是这样的。。因为compact在幕后传递它作为字符串的变量

public function index()
    {
      $question = questions::all();
      $respondent= respondents::all();
      return view('respondent.index',compact('question', 'respondent'));
    }

或者你可以这样做

public function index()
    {
      $question = questions::all();
      $respondent= respondents::all();
      return view('respondent.index')->with(['question' => $question, 'respondent'=> $respondent]);
    }
问题在于:

return view('respondent.index',compact('question', $question, 'respondent', $respondent));
view()
的第二个参数是数组

或使用:

return view('respondent.index',compact('question', 'respondent'));
压缩从传递的变量创建数组


谢谢,但错误仍然存在:“未定义变量:问题”谢谢,但错误仍然存在:“未定义变量:问题”能否将响应者的代码添加到问题中控制器
create
操作
return view('respondent.index',['question' => $question, 'respondent' => $respondent]);