PenAndPapers
PenAndPapers

Reputation: 2108

Laravel Join with where condition inside not working

So my database has two table named players and teams, each table has competitionId and teamId field so my goal is to get all players of a team base on competitionId and teamId of teams table. It only return an empty array.

public static function getTeamRoster($competitionId, $teamId) {
        return DB::table('teams as team')
                ->where('team.competitionId', $competitionId)
                ->where('team.teamId', $teamId)
                ->join('players as player', function($join){
                   $join->on('team.competitionId', '=', 'player.competitionId')
                        ->where('player.teamId', 'team.teamId');
                })
                ->get();
    }

Upvotes: 0

Views: 900

Answers (1)

Mohamed Akram
Mohamed Akram

Reputation: 2117

try the following modified function whether its brings your expected result, if not please be more specific about your requirement,

public static function getTeamRoster($competitionId, $teamId) {
    return DB::table('players AS player')
        ->join('teams AS team','player.teamId','=','team.teamId')
        ->where('team.competitionId', $competitionId)
        ->where('team.teamId', $teamId)
        ->get();
}

Upvotes: 1

Related Questions