2017-09-25 250 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

回答