laravel查询数据库没有产生预期的结果

时间:2014-02-10 15:57:27

标签: laravel laravel-4

我正在尝试使用laravel查询构建器执行此sql查询。

SELECT leagues.id, leagues.name, countries.name
FROM leagues
INNER JOIN countries ON leagues.country = countries.id

我有以下Laravel查询...

$leagueinfo = DB::table('leagues')
        ->join('countries','leagues.country', '=', 'countries.id')
        ->select('leagues.id', 'leagues.name', 'countries.name')
        ->get();

但是当我将print_r($ leagueinfo)输出到屏幕上时,我得到了这个......

Array ( [0] => stdClass Object ( [id] => 1 [name] => England ) [1] => stdClass Object ( [id] => 2 [name] => Spain ) [2] => stdClass Object ( [id] => 3 [name] => Italy ) [3] => stdClass Object ( [id] => 4 [name] => Austria ) [4] => stdClass Object ( [id] => 5 [name] => Germany ) [5] => stdClass Object ( [id] => 6 [name] => England ) )

..看起来它只是在接收我的国家。

sql查询返回正确的结果...... league.id,league.name和countries.name

有人会有任何建议,为什么信息没有通过,拜托? 感谢。

DS

1 个答案:

答案 0 :(得分:2)

如果你查看你的结果集,你实际上也有ID。发生了什么事情是你在结果集中覆盖你的列名,因为两个字段都是“name”。请尝试以下方法:

$leagueinfo = DB::table('leagues')
    ->join('countries','leagues.country', '=', 'countries.id')
    ->select('leagues.id', 'leagues.name as league', 'countries.name as country')
    ->get();