2017-02-27 10 views
1

私は雄弁を使って2つのテーブルの間でデータを取得しようとするとエラーが発生します。 くれている与えているエラー:テーブルの関係のエラー

Error: 
Trying to get property of non-object (View: 

これは、あなたがそれらを直接使用する必要があります私のアプリ情報

DB: 
survey: 
- id; 
- template_id; 
- title; 


templates: 
- id; 
- name; 
- internal_name; 

SurveyModel: 
public function theme(){ 

     return $this->hasOne(Template::class, 'template_id','id'); 
} 


View: 

@foreach($surveys->reverse() as $survey) 

     <tr> 
     <td>{{$survey->template_id->theme}}</td> 

</tr> 
@endforeach 

答えて

1

です:

<td>{{$survey->theme}}</td> 

そして、それは、関連Templateモデルを返しますが代わりに、あなたが望む属性を得ることができます。例:

<td>{{$survey->theme->name}}</td> 

希望すると便利です。