提问者:小点点

Laravel 5.6 with Count and where语句


我用的是laravel 5.6

我有3张桌子:球员、游戏和game_player。

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

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

当玩家赢得游戏时,有没有办法检索游戏的游戏计数?(在playersControler的索引动作中)我不确定如何做到这一点。有人能帮忙吗?

游戏表迁移

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

game_player表迁移

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);
}

我想要的结果是玩游戏并赢得游戏。

游戏者

@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

更新

我不认为我们可以把它看作这个问题的重复(Laravel在withCount方法上使用where子句),因为我也使用了多对多关系。

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

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

我得到一个错误:

SQLSTATE[42S22]:未找到列:1054“having子句”中的未知列“winner”(SQL:selectplayers,(在games上从games内部连接games\u player中选择count()<代码>id=游戏玩家<代码>游戏id其中玩家<代码>id=游戏玩家<代码>玩家id)作为游戏计数来自玩家拥有赢家=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);

叶片指数

@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

我得到了这个(不是我真正想要的,但我想是得到了):


共1个答案

匿名用户

因为您在游戏表上定义了一个外键,所以您已经在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());
}