Laravel 5.6 withCount 和 where 语句

Laravel 5.6 withCount and where statement

我正在使用 laravel 5.6。

我有 3 个 table:玩家、游戏和 game_player table。

在 PlayersController 的索引操作中检索每个玩家的游戏计数很容易:

//Get game count of every player
$players = Player::withCount('games')->get();

有没有办法在玩家赢得比赛时检索比赛的比赛次数? (在玩家控制器的索引动作中)我不知道该怎么做。有人可以帮忙吗?

游戏table迁移

$table->integer('winner')->unsigned()->index();
$table->foreign('winner')->references('id')->on('players')->onDelete('cascade');

game_player table迁移

table->integer('game_id')->unsigned()->nullable();
$table->foreign('game_id')->references('id')->on('games')->onDelete('cascade');

$table->integer('player_id')->unsigned()->nullable();
$table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');

游戏模型关系

public function players(){
    return $this->belongsToMany('App\Player')->withTimestamps();
}

//this is for the winner of the game
public function player()
{
    return $this->hasOne('App\Player');
}

玩家模型关系

public function games(){
    return $this->belongsToMany('App\Game')->withTimestamps();
}

//the game the player has won
public function game()
{
    return $this->belongsTo('App\Game');
}

玩家控制器

public function index()
{
    $players = Player::all();

    //Get game count of every player
    $players = Player::withCount('games')->get();

    /*Load the view and pass the groups*/
    return \View::make('players.index')->with('players', $players);
}

我想要的结果是玩游戏(有效)并赢得游戏。

玩家 > 索引 blade

@foreach($players as $player)
   <p>{{ $player->id }}</p>
   <p>{{ $player->firstname }} {{ $player->lastname }}</p>
   <ul>
      <li>Played games: {{ $player->games_count }}</li>
      <li>Won games: </li>
   </ul>
@endforeach

更新

我不认为我们可以将它视为这个问题的副本 (),因为我也在使用多对多关系。

如果我使用的代码不正确,因为 1 需要是动态的 $id:

$players = Player::withCount('games')
     ->having('winner', '=', 1)
     ->get();

我收到错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'winner' in 'having clause' (SQL: select players., (select count() from games inner join game_player on games.id = game_player.game_id where players.id = game_player.player_id) as games_count from players having winner = 1)

更新 2

当我使用这段代码时:

控制器

$players = Player::all();

//Get game count of every player
$players = Player::withCount('games')->get();

$wongames = Player::withCount(['games' => function($query) { $query->where('winner', '=', 5); }])->get();

//$players = Player::withCount('games')->where('winner', '=', 1);

/*Load the view and pass the groups*/
return \View::make('players.index')->with('players', $players)->with('wongames', $wongames);

blade 指数

@foreach($players as $player)
   <p>{{ $player->id }}</p>
   <p>{{ $player->firstname }} {{ $player->lastname }}</p>
   <ul>
      <li>Played games: {{ $player->games_count }}</li>
         @foreach($wongames as $wongame)
            <li>Won games: {{ $wongame->games_count }}</li>
         @endforeach
   </ul>
@endforeach

我明白了(不是我真正想要的,但我想达到了):

由于您在游戏 table 上定义了一个外键,因此您在 PlayerGame 之间已经建立了一对多的关系。尝试将以下关系添加到您的 Player 模型:

// Player.php
public function won()
{
    // must specify the foreign key because it is not the usual `_id` convention.
    return $this->hasMany(Game::class, 'winner');
}

然后在每个播放器上访问它:

@foreach($players as $player)
    {{ $player->won->count() }}
@endforeach

与其在视图文件中查询,不如在控制器中执行以下操作:

public function index()
{
    /*Load the view and pass the groups*/
    return \View::make('players.index')->with('players', Player::with('won')->get());
}