【发布时间】:2021-03-20 21:07:28
【问题描述】:
我需要为每个联赛创建积分榜。关系:
团队模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
protected $fillable = ['name', 'league_id', 'stadium'];
public function league() {
return $this->belongsTo('App\League');
}
public function players() {
return $this->hasMany('App\Player');
}
public function matches() {
return $this->hasMany('App\Match');
}
}
匹配模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Team;
class Match extends Model
{
protected $fillable = ['round', 'match_date'];
protected $guarded = ['league_id', 'home_team_id', 'away_team_id'];
public function leagues() {
return $this->belongsTo('App\League');
}
public function homeTeam() {
return $this->belongsTo('App\Team', 'home_team_id');
}
public function awayTeam() {
return $this->belongsTo('App\Team', 'away_team_id');
}
public function score()
{
return $this->hasOne('App\Score', 'match_id');
}
}
在获取所有联赛球队时创建积分榜功能。 排名应该有比赛,胜利,平局,失败。有人可以帮忙,如何获取这些数据?
public function standings(League $league, Team $team) {
$teams = Team::with('league')->where('league_id', $league->id)->get();
//dd($team_matches);
return view('admin.leagues.standings')->with('teams',$teams);
}
【问题讨论】:
-
您如何评价胜负?这些数据是存储在匹配表还是其他地方?
-
比赛与分数有关系(match_id, home_team_score, away_team_score)
标签: arrays laravel sorting multidimensional-array eloquent