在控制器中,我创建了一个集合:
$playershome = App\Team::where('id', $session->team1_id)->with('players')->first();
并调用我的观点:
return view('member.home', compact('user', 'session', 'playershome', 'playersaway'));
在我的团队模型中,我有:
public function players(){
return $this->belongsToMany(User::class, 'team_users');
}
集合被创建:
但我不知道如何在我的视图中检索“玩家”项目。我尝试:
@foreach($playershome->players as $player)
但我得到了:
Invalid argument supplied for foreach()
怎么了?
更新视图:
<div class="mt-body">
<h3 class="mt-body-title"> {{$session->team1Id->name}} </h3>
<p class="mt-body-description"> It is a long established fact that a reader will be distracted </p>
<ul class="mt-body-stats">
@foreach($playershome->players as $player)
<li class="font-green">
<img src="/storage/{{$player->avatar}}"></li>
@endforeach
</ul>
<div class="mt-body-actions">
<div class="btn-group btn-group btn-group-justified">
<a href="javascript:;" class="btn">
<i class="icon-bubbles"></i> Punti </a>
<a href="javascript:;" class="btn ">
<i class="icon-social-twitter"></i> K/D </a>
</div>
</div>
</div>