Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 拉威尔在哪里和哪里_Laravel_Eloquent - Fatal编程技术网

Laravel 拉威尔在哪里和哪里

Laravel 拉威尔在哪里和哪里,laravel,eloquent,Laravel,Eloquent,我正试图找出如何以雄辩的方式创建以下查询: SELECT * FROM `pvp_battles` WHERE (player1_id = 2 || player2_id =2) && winner !=2 2是玩家id。这是我做的: $profile->loses = PvpBattle::where('player1_id',$profile->id) ->orWhere('player2_id',$profile->id)

我正试图找出如何以雄辩的方式创建以下查询:

SELECT * 
FROM  `pvp_battles` 
WHERE (player1_id = 2 || player2_id =2) && winner !=2
2是玩家id。这是我做的:

$profile->loses = PvpBattle::where('player1_id',$profile->id)
        ->orWhere('player2_id',$profile->id)
        ->where('winner','!=',$profile->id)->count();
可悲的是,它显示计数是49,而它是25。 出什么问题了?

以下是修复方法:

$profile->loses = PvpBattle::where(function($q) use($profile) {
  return $q
           ->where('player1_id', $profile->id)
           ->orWhere('player2_id', $profile->id);
})->where('winner', '!=', $profile->id)->count();
或:

怎么样

$profile->loses = PvpBattle::where(function($query) use ($profile){
    $query->where('player2_id', '=', $profile->id)
    ->orWhere('player1_id', '=', $profile->id);
})
->where('winner','!=',$profile->id)->count();
$profile->loses = PvpBattle::where(function($query) use ($profile){
    $query->where('player2_id', '=', $profile->id)
    ->orWhere('player1_id', '=', $profile->id);
})
->where('winner','!=',$profile->id)->count();