2017-09-25 1 views
0

나는 type_id 열이있는 질문 표가 있습니다. 여기에 정의 된 값은 QuestionTypes 모델과 관련됩니다.Laravel Eloquent relationships - 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

은'belongsTo를()'대신 hasOne의'방법을()'를 사용하십시오 . – Camilo

답변