laravel5.6带count和where语句

35g0bw71  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(478)

我用的是拉维5.6。
我有三张table:玩家,游戏和玩家桌。
在PlayerController的索引操作中,检索每个玩家的游戏计数很容易:

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

当玩家赢了游戏后,有没有办法找回游戏的游戏计数(在playerscontroller的索引操作中)我不确定如何执行此操作。有人能帮忙吗?
游戏表迁移

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


游戏机表迁移

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]:找不到列:“having子句”中的1054未知列“winner”(sql:select) players ,(从 games 内部连接 game_playergames . id = game_player . game_id 哪里 players . id = game_player . player_id )作为 games_countplayerswinner = 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

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

jchrr9hc

jchrr9hc1#

因为您在games表上定义了一个外键,所以 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

理想情况下,您应该在控制器中执行以下操作,而不是在视图文件中进行查询:

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

相关问题