I have this sql query that works perfectly (returns one row):
SELECT player_stats.*, opposing_team.nombre AS opposing_team_name
FROM player_stats
JOIN partidos ON partidos.id = player_stats.partido_id
JOIN equipos AS player_team ON (
(player_team.id = partidos.equipo_local_id OR player_team.id = partidos.equipo_visitante_id)
AND EXISTS (
SELECT 1
FROM equipos_jugadores
WHERE equipo_id = player_team.id
AND jugador_id = player_stats.jugador_id
)
)
JOIN equipos AS opposing_team ON (
opposing_team.id <> player_team.id
AND (
opposing_team.id = partidos.equipo_local_id
OR
opposing_team.id = partidos.equipo_visitante_id
)
)
WHERE player_stats.jugador_id = 337;
I want to change it to eloquent and i am using this code:
PlayerStat::query()
->join('partidos', 'partidos.id', '=', 'player_stats.partido_id')
->join('equipos as player_team', function ($join) {
$join->on(function ($query) {
$query->where('player_team.id', '=', 'partidos.equipo_local_id')
->orWhere('player_team.id', '=', 'partidos.equipo_visitante_id');
})
->whereExists(function ($query) {
$query->from('equipos_jugadores')
->where('equipo_id', '=', 'player_team.id')
->where('jugador_id', '=', 'player_stats.jugador_id');
});
})
->join('equipos as opposing_team', function ($join) {
$join->on('opposing_team.id', '<>', 'player_team.id')
->where(function ($query) {
$query->where('opposing_team.id', '=', 'partidos.equipo_local_id')
->orWhere('opposing_team.id', '=', 'partidos.equipo_visitante_id');
});
})
->select('player_stats.*', 'opposing_team.nombre as opposing_team_name')
->where('player_stats.jugador_id', '=', $this->jugador_id);
but it doesnt give any rows, any ideas?