我尝试在 Laravel 中链接三个表。到目前为止,我的代码是:
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