Php Laravel:使用where子句按id计数行和分组

Php Laravel:使用where子句按id计数行和分组,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我目前正在使用php框架Laravel创建我的第一个项目。我使用的最新版本是8.9.0,我遇到了一个自己无法解决的问题。我需要为每场比赛创建一份最佳得分手名单。我的数据库表如下所示: Players id | player name 1 | Player A 2 | Player B 3 | Player C Tournaments id | tournament 1 | Tournament A 2 | Tournament B 3 | Tournament C Tourna

我目前正在使用php框架Laravel创建我的第一个项目。我使用的最新版本是8.9.0,我遇到了一个自己无法解决的问题。我需要为每场比赛创建一份最佳得分手名单。我的数据库表如下所示:

Players
id | player name 
1  | Player A
2  | Player B
3  | Player C

Tournaments
id | tournament
1  | Tournament A
2  | Tournament B
3  | Tournament C

TournamentStats
id  | game   | player_id  |  goal  | tournament_id
1   |  1     |  1         |  1     |  1
2   |  1     |  1         |  1     |  1
3   |  1     |  2         |  1     |  1
4   |  2     |  3         |  1     |  1
5   |  2     |  2         |  1     |  1
6   |  2     |  2         |  1     |  1
7   |  1     |  2         |  1     |  2
8   |  1     |  2         |  1     |  2
9   |  1     |  3         |  1     |  2
10  |  2     |  2         |  1     |  2
11  |  2     |  2         |  1     |  2
12  |  2     |  3         |  1     |  2
Controller 

$goals = Player::all()->sortByDesc('goals');
 return view('tournament.goals')->with('goals', $goals);

Model 
  public function getGoalsAttribute()
{
    return TournamentStats::where(function($query) {
        $query->where('player_id', $this->attributes['id'])
    })->count();
}
 $goals = Player::all()->where('tournament_id', $id)->sortByDesc('goals');
我的目标是,当比赛A页面打开时,它应该显示:

Player B 3
Player A 1
Player C 1
如果是B,那么

Player B 4
Player C 2
Player A 0
我试过这样做:

Players
id | player name 
1  | Player A
2  | Player B
3  | Player C

Tournaments
id | tournament
1  | Tournament A
2  | Tournament B
3  | Tournament C

TournamentStats
id  | game   | player_id  |  goal  | tournament_id
1   |  1     |  1         |  1     |  1
2   |  1     |  1         |  1     |  1
3   |  1     |  2         |  1     |  1
4   |  2     |  3         |  1     |  1
5   |  2     |  2         |  1     |  1
6   |  2     |  2         |  1     |  1
7   |  1     |  2         |  1     |  2
8   |  1     |  2         |  1     |  2
9   |  1     |  3         |  1     |  2
10  |  2     |  2         |  1     |  2
11  |  2     |  2         |  1     |  2
12  |  2     |  3         |  1     |  2
Controller 

$goals = Player::all()->sortByDesc('goals');
 return view('tournament.goals')->with('goals', $goals);

Model 
  public function getGoalsAttribute()
{
    return TournamentStats::where(function($query) {
        $query->where('player_id', $this->attributes['id'])
    })->count();
}
 $goals = Player::all()->where('tournament_id', $id)->sortByDesc('goals');
这是可行的,但它显示了我的玩家表中的所有玩家。我只需要展示那些在特定比赛中得分的球员。我知道我需要设置where子句,但我不知道在哪里。我尝试在我的模型中添加如下内容:

 public function getGoalsAttribute($tournament)
 {
  return TournamentStats::where(function($query) {
  $query->where('player_id', $this->attributes['id'])
        ->where('tournament_id', $tournament);
  })->count();
}
在我的控制器中,如下所示:

Players
id | player name 
1  | Player A
2  | Player B
3  | Player C

Tournaments
id | tournament
1  | Tournament A
2  | Tournament B
3  | Tournament C

TournamentStats
id  | game   | player_id  |  goal  | tournament_id
1   |  1     |  1         |  1     |  1
2   |  1     |  1         |  1     |  1
3   |  1     |  2         |  1     |  1
4   |  2     |  3         |  1     |  1
5   |  2     |  2         |  1     |  1
6   |  2     |  2         |  1     |  1
7   |  1     |  2         |  1     |  2
8   |  1     |  2         |  1     |  2
9   |  1     |  3         |  1     |  2
10  |  2     |  2         |  1     |  2
11  |  2     |  2         |  1     |  2
12  |  2     |  3         |  1     |  2
Controller 

$goals = Player::all()->sortByDesc('goals');
 return view('tournament.goals')->with('goals', $goals);

Model 
  public function getGoalsAttribute()
{
    return TournamentStats::where(function($query) {
        $query->where('player_id', $this->attributes['id'])
    })->count();
}
 $goals = Player::all()->where('tournament_id', $id)->sortByDesc('goals');
方法1:

为球员和锦标赛模型的TournamentStat模型创建关系

public function player()
{
    return $this->belongsTo(\App\Player::class, 'player_id');
}

public function tournament()
{
    return $this->belongsTo(\App\Tournament::class, 'tournament_id');
}
现在使用groupBy进行查询

$dumpData = \App\TorunamentStats::with([
    'player',
    'tournament'
])
    ->where('tournament_id', 1)
    ->selectRaw('id,tournament_id,count(player_id),player_id')
    ->groupBy('player_id')
    ->get();

dd($dumpData->toArray());
方法2: 使用数据库


注意:您可能会得到groupby错误,我还没有正确理解此错误的修复方法,但是可以通过关闭database.php for mysql上的strict来修复

利用多对多关系。这对你来说会更容易。它是相当广阔和漫长的,给你一个新的世界。但是我建议你使用多对多关系,你可以把TournamentStats作为一个数据透视表。当锦标赛a的页面打开时,它应该显示我看到6行,但是玩家的值之和为5。。。打字错误?你在这方面有什么关系吗?谢谢你的帮助。