Ok, can anyone help me with this scenerio? I want to make a loop of Divisions and an inner loop of all the teams in that division using relationships in Eloquent. Been googling and expermenting but can't get the right results.
My tables are as such:
Divisions
division_id INT PK
division_name VARCHAR
...
Teams
team_id INT PK
team_name VARCHAR
...
division_team (pivot table)
id INT PK
division_id INT FK
team_id INT FK
My Models are as such:
DivisionTeamPivot
class DivisionTeamPivot extends \Eloquent {
protected $fillable = [];
protected $table = 'division_team';
public function team(){
return $this->belongsTo('Team');
}
public function division(){
return $this->belongsTo('Division');
}
Divisons
class Division extends \Eloquent {
protected $primaryKey = 'division_id';
public function teams(){
return $this->hasMany('Team','division_team','division_id','team_id');
}
}
Teams
class Team extends \Eloquent {
protected $primaryKey = 'team_id';
public function division(){
return $this->belongsTo('Division','division_team','team_id','division_id');
}
}
My Controller has:
$divisions = DivisionTeamPivot::with('Team','Division')->get();
My View:
<section class="panel">
<header class="panel-heading">
<h5>{{$division->division->division_name}} Division {{$i}}</h5>
</header>
<table class="table">
<thead>
<tr>
<th></th>
<th>Team Name</th>
<th class="center">Total Points</th>
<th class="center">Rank</th>
</tr>
</thead>
<tbody>
@foreach($division->team as $team)
<tr>
<td width="75"><img src="/img/logos/{{team->team_logo}}" width="50"></td>
<td><a href="/teams/details"> {{team->team_name}}</a></td>
<td class="center">1,345</td>
<td class="center">1</td>
</tr>
@foreach
</tbody>
</table>
</section>
What I want to do is loop over the divisions and then sub loop the teams that are in those divisions. I saw someone on here to make a pivot table model but when I use it, it loops over all the same divisions based off the division_id table in the pivot but I get the name. Its creating one team per duplicate division. I need them grouped based on the divisions they are in. And, actually, I still can't get the teams name to show. I also really want the eager loading to be working so I get the most efficient queries I can. Thanks!!