0
我正在使用以下模型:游戏,用户和播放器,其中播放器扩展用户。在这种情况下,游戏和玩家的关系为N:N,用户扩展Confide。所以,代码表示,如:Laravel 4多对多关系不显示相关数据
应用程序/模型/ Game.php
<?php
class Game extends Eloquent {
protected $guarded = array();
protected $table = 'users';
public $timestamps = false;
public function players(){
return $this->belongsToMany('Player');
}
}
应用程序/模型/ user.php的
<?php
use Zizaco\Confide\ConfideUser;
class User extends ConfideUser {
protected $table = 'users';
public $timestamps = false;
protected $hidden = array('password');
//Here go original Confide functions
}
应用程序/模型/ Player.php
<?php
class Player extends User{
public function games(){
return $this->belongsToMany('Game');
}
}
我已经填充了名为game_player的数据透视表。随着玩家扩展用户,透视表是这样的:
game_player
id
game_id
user_id
但是,如果我跑这个超级简单的例子,我有一个错误:
$game = Game::find($gameId);
$player = $game->players(); //Error in this line
错误
Call to undefined method Illuminate\Database\Query\Builder::players()
我很困惑,因为我没有error about pivot table was not found,所以我可以认为关系创建的很好。
在另一方面,如果我的var_dump游戏变种我得到:
的var_dump($比赛)
object(Game)[181]
protected 'table' => string 'games' (length=5)
//more var info about DB
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'guarded' =>
如果关系将产生罚款...不应该关系变种有任何数据?
谢谢!