Laravel hasManyThrough - 未返回所有预期结果
Laravel hasManyThrough - not returning all expected results
我在一个 Laravel 项目中有两个 table survey
和 question
。我想创建一个 hasManyThrough eloquent 关系,这样我就可以遍历属于调查的所有问题。
调查Table:
----------------
| id | title |
----------------
| 1 | fruit |
----------------
问题Table:
----------------
| id | title |
----------------
| 1 | apple? |
| 3 | banana? |
| 4 | kiwi? |
| 5 | pear? |
----------------
调查问题table:
--------------------------------
| id | survey_id | question_id |
--------------------------------
| 1 | 1 | 1 |
| 1 | 1 | 4 |
| 1 | 1 | 5 |
--------------------------------
在我的调查模型中,我目前有以下
public function questions()
{
return $this->hasManyThrough(
Questions::class,
SurveyQuestion::class,
'question_id', // Foreign key on surveyquestion table...
'id', // Foreign key on questions table...
'id', // Local key on survey table...
'survey_id' // Local key on surveyquestion table...
);
}
在我的 SurveyQuestion 模型中我有:
public function survey()
{
return $this->belongsTo(Survey::class);
}
public function question()
{
return $this->belongsTo(Questions::class);
}
然而,当我遍历 $survey->questions
时,它只返回 question_id 为 1 时的行?我做错了什么?
这是一个多对多的关系。
调查模型
public function questions()
{
return $this->belongsToMany(Question::class, 'survey_question');
}
问题模型
public function surveys()
{
return $this->belongsToMany(Survey::class, 'survey_question');
}
那你可以
$survey = Survey::with('questions')->find($id);
@foreach($survey->questions as $question)
//...
@endforeach
参考docs
按照 hasManyThrough
的结构,您需要具有这样的结构
Survey
id - integer
name - string
Question
id - integer
survey_id - integer
name - string
SurveyQuestion
id - integer
question_id - integer
title - string
但是你的中间没有外键table先解决这个问题然后我想你会摆脱这个问题。
我在一个 Laravel 项目中有两个 table survey
和 question
。我想创建一个 hasManyThrough eloquent 关系,这样我就可以遍历属于调查的所有问题。
调查Table:
----------------
| id | title |
----------------
| 1 | fruit |
----------------
问题Table:
----------------
| id | title |
----------------
| 1 | apple? |
| 3 | banana? |
| 4 | kiwi? |
| 5 | pear? |
----------------
调查问题table:
--------------------------------
| id | survey_id | question_id |
--------------------------------
| 1 | 1 | 1 |
| 1 | 1 | 4 |
| 1 | 1 | 5 |
--------------------------------
在我的调查模型中,我目前有以下
public function questions()
{
return $this->hasManyThrough(
Questions::class,
SurveyQuestion::class,
'question_id', // Foreign key on surveyquestion table...
'id', // Foreign key on questions table...
'id', // Local key on survey table...
'survey_id' // Local key on surveyquestion table...
);
}
在我的 SurveyQuestion 模型中我有:
public function survey()
{
return $this->belongsTo(Survey::class);
}
public function question()
{
return $this->belongsTo(Questions::class);
}
然而,当我遍历 $survey->questions
时,它只返回 question_id 为 1 时的行?我做错了什么?
这是一个多对多的关系。
调查模型
public function questions()
{
return $this->belongsToMany(Question::class, 'survey_question');
}
问题模型
public function surveys()
{
return $this->belongsToMany(Survey::class, 'survey_question');
}
那你可以
$survey = Survey::with('questions')->find($id);
@foreach($survey->questions as $question)
//...
@endforeach
参考docs
按照 hasManyThrough
的结构,您需要具有这样的结构
Survey id - integer name - string
Question id - integer survey_id - integer name - string
SurveyQuestion id - integer question_id - integer title - string
但是你的中间没有外键table先解决这个问题然后我想你会摆脱这个问题。