This really isn't a laravel question as much as it is an SQL question:
$games = DB::table('games')
->leftJoin('teams AS awayteam', 'awayteams.id', '=', 'games.away_team')
->leftJoin('teams AS hometeam', 'hometeams.id', '=', 'games.home_team')
->select('games.id','awayteam.name', 'hometeam.name')->orderBy('games.game_date')
->get();
You might also want to look into Eloquent scopes, since this would probably be used often, and scopes allow for encapsulating logic into your model
Thanks jmichaelterenin. I'm getting this error: Unknown column 'teams.name' in 'field list'
I forgot to change to the pseudo table name, edited it in the same reply
jmichaelterenin, thanks for the help. I'm only getting the home team name in the output:
[
{
id: "10",
name: "Blue Team",
game_date: "2014-10-31 13:30:00"
},
{
id: "11",
name: "Red Team",
game_date: "2014-11-06 16:15:00"
}
]
got it.. here is the final code:
$games = DB::table('games')
->leftJoin('teams AS awayteam', 'awayteams.id', '=', 'games.away_team')
->leftJoin('teams AS hometeam', 'hometeams.id', '=', 'games.home_team')
->select('games.id','awayteam.name as away', 'hometeam.name as home')->orderBy('games.game_date')
->get();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community