arrays - 如何获取每个团队的结果 laravel
问题描述
我需要为每个联赛创造积分榜。关系:
团队模型:
<?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);
}
解决方案
获取所有数据的一种方法是
public function standings(League $league, Team $team) {
$teams = Team::where('league_id', $league->id)
->with([
'league.matches.score'
])
->get();
//dd($team_matches);
return view('admin.leagues.standings')->with('teams',$teams);
}
然后在视图中渲染时,您可以根据分数计算输赢。
推荐阅读
- wordpress - 如何根据wordpress中的当前帖子自动选择一个值?
- python - Can´t excecute view action in Django
- react-router-dom - 如何让这个滑出导航菜单与 react-router 和 framer-motion 一起使用?
- arrays - for循环中有两个if语句?
- javascript - 我如何使用结构扩展我的 jsx 元素
- r - 删除纵向数据中的不连续时间点
- python - Python jupyter 网络服务器在 3-5 秒后停止
- python - 然后在条件下读取 sheet_names 列表
- python - SqlAlchemy 递归查询
- sql - IF 函数格式 SQL 出错:不支持错误 BOOL、STRING、BOOL,仅 IF(BOOL、ANY、ANY)