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
函數。
你是否厭倦了循環'$ own'和使用屬性的名稱,或獲得第一行'所有權::用('遊戲','類型') - > first()'然後使用屬性名稱。 – Usman
是的,我做到了。對於'$ own = Ownership :: with('games','type') - > first();'然後是「foreach」,它會產生「111」。當我嘗試去做'echo $ game-> games-> title;'我得到「試圖獲得非對象的屬性」。 –