2013-05-16 40 views
0

我尝试在Laravel链三个表。到目前为止我的代码是:Laravel 4 - 急切的负载不打印

class Ownership extends Eloquent { 
    protected $table = 'games_users'; 

    public function games() { 
     return $this->belongsToMany('Game','games_users','owntype_id','games_id'); 
    } 

    public function type() { 
     return $this->hasMany('Owntype','id'); 
    } 
} 

而且我的控制器:

$own = Ownership::with('games','type')->get(); 

随着print_r,似乎工作,但我不能打印。我该怎么做?

编辑

随着代码

$own = Ownership::with('games','type')->get(); 

foreach($own as $game) { 
    echo $game->games; 
} 

我得到这个:

[{"id":1,"title":"Gra","pivot":{"owntype_id":1,"games_id":1}}][{"id":2,"title":"Gra 2","pivot":{"owntype_id":2,"games_id":2}}] 

的问题是:我不知道该怎么回应title,它并没有我没有附加type函数。

+0

你是否厌倦了循环'$ own'和使用属性的名称,或获得第一行'所有权::用('游戏','类型') - > first()'然后使用属性名称。 – Usman

+0

是的,我做到了。对于'$ own = Ownership :: with('games','type') - > first();'然后是“foreach”,它会产生“111”。当我尝试去做'echo $ game-> games-> title;'我得到“试图获得非对象的属性”。 –

回答

0

我有同样的问题,在您的悬而未决的问题来了,但尽量echo $game->games['title'],因为它是一个数组