无法通过 Laravel Eloquent 关系检索正确的结果
Trouble Retrieving Proper Results with Laravel Eloquent Relations
我无法通过 Eloquent 关系检索特定结果集。
型号:
Application(table: application) [id, title, active]
问题(table: 问题) [id, application_id, question_text, helper_text , question_type]
QuestionType(table: question_type) [id, type]
粗体 = 主键,斜体 = 外键
关系:
一个Application
有很多Questions
.
许多 Questions
可以属于一个 Application
。
一个Question
有一个QuestionType
(引用question_type FK)
一个 QuestionType
可以属于多个 Questions
(通过引用它的 id 作为 question_type)
QuestionType
是静态的 table,永远不会添加或删除值,但是类型属性可以更改。
我希望能够做这样的事情:
$application = Application:find($application_id);
$questions = $application->questions()->get();
并用从 QuestionType
模型中提取的适当类型替换 question_type
。
我查看了 Laravel 和 Eloquent 文档,在 IRC 上提问,并查看了其他 Whosebug 文章,但找不到有用的答案。我认为让我失望的是我的非常规外键 question_type
在我的 Question
table 中。我得到了它一次工作,异常 question_type
被替换为 QuestionType
数组(这不起作用)。
我说先把这个关系加到Question
:
public function type(){
return $this->belongsTo('QuestionType', 'question_type');
}
然后像平常一样使用它:
$question = Question::find(1);
$question->type;
对于应用程序的所有问题(预加载)
$application = Application:find($application_id);
$questions = $application->questions()->with('type')->get();
编辑
要仅获取类型的实际名称(名为 type
的列),您可以添加一个 attribute accessor。然而,命名变得有点困难。如果您真的不想将外键名称更改为 question_type_id
,我建议您这样做:
public function typeRelation(){
return $this->belongsTo('QuestionType', 'question_type');
}
public function getTypeAttribute(){
if(is_null($this->typeRelation)) return null;
return $this->typeRelation->type;
}
属性访问器允许您使用$question->type
并直接获取相关模型的属性。预加载时不要忘记调整关系名称:with('typeRelation')
我无法通过 Eloquent 关系检索特定结果集。
型号:
Application(table: application) [id, title, active]
问题(table: 问题) [id, application_id, question_text, helper_text , question_type]
QuestionType(table: question_type) [id, type]
粗体 = 主键,斜体 = 外键
关系:
一个Application
有很多Questions
.
许多 Questions
可以属于一个 Application
。
一个Question
有一个QuestionType
(引用question_type FK)
一个 QuestionType
可以属于多个 Questions
(通过引用它的 id 作为 question_type)
QuestionType
是静态的 table,永远不会添加或删除值,但是类型属性可以更改。
我希望能够做这样的事情:
$application = Application:find($application_id);
$questions = $application->questions()->get();
并用从 QuestionType
模型中提取的适当类型替换 question_type
。
我查看了 Laravel 和 Eloquent 文档,在 IRC 上提问,并查看了其他 Whosebug 文章,但找不到有用的答案。我认为让我失望的是我的非常规外键 question_type
在我的 Question
table 中。我得到了它一次工作,异常 question_type
被替换为 QuestionType
数组(这不起作用)。
我说先把这个关系加到Question
:
public function type(){
return $this->belongsTo('QuestionType', 'question_type');
}
然后像平常一样使用它:
$question = Question::find(1);
$question->type;
对于应用程序的所有问题(预加载)
$application = Application:find($application_id);
$questions = $application->questions()->with('type')->get();
编辑
要仅获取类型的实际名称(名为 type
的列),您可以添加一个 attribute accessor。然而,命名变得有点困难。如果您真的不想将外键名称更改为 question_type_id
,我建议您这样做:
public function typeRelation(){
return $this->belongsTo('QuestionType', 'question_type');
}
public function getTypeAttribute(){
if(is_null($this->typeRelation)) return null;
return $this->typeRelation->type;
}
属性访问器允许您使用$question->type
并直接获取相关模型的属性。预加载时不要忘记调整关系名称:with('typeRelation')