我使用的是 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');
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');
}
玩家 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);
}
我想要的结果是玩游戏(正在运行)并赢得游戏。
玩家>索引 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() fromgames
inner joingame_player
ongames
.id
=game_player
.game_id
whereplayers
.id
=game_player
.player_id
) asgames_count
fromplayers
havingwinner
= 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
我明白了(不是我真正想要的,但我想到达那里):
最佳答案
由于您在游戏表上定义了外键,因此 Player
和 Game
之间已经存在一对多关系。尝试将以下关系添加到您的 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/