php - Laravel 5.6 withCount 和 where 语句

标签 php mysql laravel where-clause laravel-5.6

我使用的是 Laravel 5.6。

我有 3 个表:players、games 和 game_player 表。

通过 PlayersController 的索引操作可以轻松检索每个玩家的游戏计数:

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

当玩家赢得游戏时,有没有办法检索游戏的游戏计数? (在玩家 Controller 的索引操作中)我不知道如何做到这一点。有人可以帮忙吗?

游戏表迁移

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

enter image description here

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

enter image description here

游戏模型关系

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

玩家 Controller

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

我想要的结果是玩游戏(正在运行)并赢得游戏。

result

玩家>索引 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

更新

我认为我们不能将其视为此问题( Laravel using where clause on a withCount method )的重复,因为我也在使用多对多关系。

如果我使用的代码不正确,因为 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

当我使用此代码时:

Controller

$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

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

enter image description here

最佳答案

由于您在游戏表上定义了外键,因此 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

理想情况下,您应该在 Controller 中执行以下操作,而不是在 View 文件中查询:

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

关于php - Laravel 5.6 withCount 和 where 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49865193/

相关文章:

mysql - 使用 laravel 在 AUTO_INCREMENT 字段中设置 id 值

php - 如何在 Laravel 的 Controller 中捕获异常?

php - 来自 hasMany 关系的 Laravel 预加载模型

php - 读取带括号的 header 时,PHP 的 imap_fetch_overview() 函数是否有错误?

php - Joomla 组件 + 友好的 url

mysql - 带计数的 Laravel DB 查询

Javascript修改mySQL数据/表

php - 把自己设定为初学者练习PHP有什么好的项目?

php - 使用 htaccess 获得良好的 SEO 链接

php - 在 while 循环内添加值