2015-05-27 129 views
0

我想,當我得到的所有結果以訪問模型的關係,但我不斷收到以下錯誤:Laravel訪問關係

Undefined property: Illuminate\Database\Eloquent\Collection::$voornaam 

代碼:

- Model : Selectie.php 

public function User() 
{ 
    return $this->hasMany('User', 'id', 'user_id'); 
} 

- Controller 

$selectie = Selectie::where('wedstrijd_id', '=', $id)->get(); 

return View::make('base.match.show')->with('selectie', $selectie); 

- View 

@foreach($selectie as $sel) 
    {{ $sel->user->voornaam }} 
@endforeach 

回答

0

你有一到多Selectie模型中的關係。這意味着,$ sel->用戶返回集合數組,你不能達到這樣的數組屬性。

@foreach($selectie as $sel) 
    @foreach($sel->user as $user) 
      {{ $user->voornaam }} 
    @endforeach 
@endforeach 

嘗試這樣的,否則你必須改變你的Selectie模式 - 與用戶關係模型類型一個一對一