Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel在视图中显示一对多关系_Php_Laravel_Model View Controller_Laravel 5.1 - Fatal编程技术网

Php Laravel在视图中显示一对多关系

Php Laravel在视图中显示一对多关系,php,laravel,model-view-controller,laravel-5.1,Php,Laravel,Model View Controller,Laravel 5.1,我目前正在为我的网站添加一个功能,它可以提问并提供多项选择答案 我的答案模型的代码如下: protected $table = 'answer'; protected $primaryKey = 'answer_id'; protected $fillable = ['question_id']; public function question() { return $this->belongsTo('App\Question','question_id')->dis

我目前正在为我的网站添加一个功能,它可以提问并提供多项选择答案

我的答案模型的代码如下:

protected $table = 'answer';
protected $primaryKey = 'answer_id';
protected $fillable = ['question_id'];


 public function question()
{
    return $this->belongsTo('App\Question','question_id')->distinct();
}
我的观点是:

@foreach ($answers as $answer)



<h2>{{$answer->question->question}}</h2>

  <p>{{$answer->answer}}</p>

@endforeach
它的显示方式如下:

问题1

回答这里

问题1

回答这里2

问题1

回答这里3

问题2

2在这里回答

问题2

回答这里

问题2

回答这里


我只想把这个问题展示一次。我是拉威尔的新手。

你需要把这个翻过来,首先从DB那里得到你的问题:

$questions = Question::with('answers')->get();
当然,这是假设您已经在问题模型中设置了一个
hasmall
关系

在您的视图中,将有两个循环:

@foreach ($questions as $question)
    <h2>{{$question->question}}</h2>

    @foreach ($question->answers as $answer)     
        <p>{{$answer->answer}}</p>
    @endforeach

@endforeach
@foreach($questions as$question)
{{$question->question}
@foreach($问题->答案为$答案)
{{$answer->answer}

@endforeach @endforeach

请注意,首先循环问题并显示一次问题,然后循环每个问题的答案。

然后,您需要翻转此选项,并首先从数据库中获取问题:

$questions = Question::with('answers')->get();
当然,这是假设您已经在问题模型中设置了一个
hasmall
关系

在您的视图中,将有两个循环:

@foreach ($questions as $question)
    <h2>{{$question->question}}</h2>

    @foreach ($question->answers as $answer)     
        <p>{{$answer->answer}}</p>
    @endforeach

@endforeach
@foreach($questions as$question)
{{$question->question}
@foreach($问题->答案为$答案)
{{$answer->answer}

@endforeach @endforeach

请注意,您如何先循环问题并显示一次问题,然后循环每个问题的答案。

现在打印所有问题,但每个问题都有一个答案。问题1问题1回答此处问题2问题1回答此处问题2问题3问题1回答此处问题3问题4问题2-回答此处问题5问题2-回答此处问题6问题2回答此处问题3这是如何设置的?公共函数answer(){return$this->hasMany('App\answer','answer_id');}在控制器中执行
dd($questions)
,并检查数据库结果。在对视图进行故障排除之前,请确保您的关系是正确的,并且您得到了预期的结果。非常感谢!!这是我的关系!:)您提供的代码也可以完美地工作!!它现在正在打印所有问题,但每个问题都有一个答案。问题1问题1回答此处问题2问题1回答此处问题2问题3问题1回答此处问题3问题4问题2-回答此处问题5问题2-回答此处问题6问题2回答此处问题3这是如何设置的?公共函数answer(){return$this->hasMany('App\answer','answer_id');}在控制器中执行
dd($questions)
,并检查数据库结果。在对视图进行故障排除之前,请确保您的关系是正确的,并且您得到了预期的结果。非常感谢!!这是我的关系!:)您提供的代码也可以完美地工作!!