So I want to display teams and their league stats, exactly like you see on a major sports site when you go to standings, team names, records ordered by something and then grouped by their conference, division and so forth....
I have an existing db that is a bit crummy to work with, so bear with me here as I explain, all team stats are in one table called prostandings, they are all in there by season so you have multiple teams with multiple records each based on season_id. I then have a proteam table that has all the team names, one instance per team, and that has the division/conference values.
I have tried to tackle from both sides of the models to see which is a better way to list this out.
I want to do something like Conference Division Team (order this by a few order scopes)
Would it be better for me to do a get from the proteam model with a relationship to the prostandings model? Better to do a prostandings to the proteam relationship with one to many or belongs to proteam? I keep confusing myself on what the best approach is and I hit some roadblocks along the way. Anyone with some quick insight?
class team extends Model
{
protected $table = 'proteam';
public function standings()
{
return $this->HasMany(standings::class, 'Number', 'T_ID');
}
}
class standings extends Model
{
protected $table = 'prostandings';
public function team()
{
return $this->belongsTo(team::class,'T_ID','Number');
}
}
//Controller Function
$teams = team::with('standings')->get();
$teams = $teams->groupBy(function ($division) {
return $division->Division;
})->all();
return dd($teams);
This shows nicely by division grouping but my relationship shows nothing inside the collection under standings...
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community