2017-09-25 225 views
0

我有一个问题表,它有一个type_id列。此处定义的值与QuestionTypes模型相关。Laravel雄辩的关系 - hasOne?

问表:

-------------------------- 
| id | title | type_id | 
-------------------------- 
| 1 | apple? | 2  | 
| 3 | banana? | 2  | 
| 4 | kiwi? | 2  | 
| 5 | pear? | 3  | 
-------------------------- 

QuestionTypes

---------------- 
| id | title | 
---------------- 
| 1 | multi | 
| 2 | single | 
| 3 | free | 
---------------- 

在问题型号我:

public function type() 
{ 
    return $this->hasOne(QuestionType::class); 
} 

我想打印从questiontypes表标题,但是当我尝试使用$question->type->title在视图中输出我得到:

Column not found: 1054 Unknown column 'x__questiontypes.questions_id' in 'where clause' 
(SQL: select * from `x__questiontypes` where `x__questiontypes`.`questions_id` = 2 and `x__questiontypes`.`questions_id` is not null limit 1 

我混淆了关系吗?

+0

尝试使用'属于关联()'方法,而不是'hasOne()' 。 – Camilo

回答