我已经看过很多答案和文章,所有人都选择扩展Pivot或Model,但是现实生活中的用法从未被考虑过,而且从来没有比给它一个“命名”类更进一步.
这是我想要实现的一个例子:
我的主要型号:
球员模型,与球员表.
游戏模型,带游戏桌.
玩家和游戏有多对多关系,我们可以用“game_player”表称为“Party”.
现在不那么重要了,我还有一个得分/转弯的分数模型,一个Party可以有很多分数,所以得分表有一个party_id条目.
所以,这里基本上是我的课程(Laravel 5.2):
播放机
class Player extends Model { // The Games this Player is participating to. public function games() { return $this->belongsToMany(Game::class)->withPivot('id')->withTimestamps(); } // The scores belonging to this Player. public function parties() { return $this->hasManyThrough(score::class,Party::class); } // The Custom Pivot (Party) creation for this Player. public function newPivot(Model $parent,array $attributes,$table,$exists) { if ($parent instanceof Game) { return new Party($parent,$attributes,$exists); } return parent::newPivot($parent,$exists); } }
游戏
class Game extends Model { // The Players participating into this Game. public function players() { return $this->belongsToMany(Player::class)->withPivot('id')->withTimestamps(); } // The scores belonging to this Party. public function parties() { return $this->hasManyThrough(score::class,Party::class); } // The Custom Pivot (Party) creation for this Game. public function newPivot(Model $parent,$exists) { if ($parent instanceof Player) { return new Party($parent,$exists); } }
派对
class Party extends Pivot { protected $table = 'game_player'; // The Player this Party belongs to. public function player() { return $this->belongsTo(Player::class); } // The Game this Party belongs to. public function event() { return $this->belongsTo(Game::class); } // The scores belonging to this Party. public function scores() { return $this->hasMany(score::class); } }
得分了
class score extends Model { // The Party this score belongs to (has "party_id" column). public function party() { return $this->belongsTo(Party::class); } }
现在,我有两组不同的问题,取决于我是否在Party上扩展Model或Pivot:
1)当Party扩展Pivot时:
>不能做类似Party :: count()的事情,任何适用于模型的东西.
>不能做像Party :: where(“player_id”,$player-> id)之类的东西……基本上我希望派对上有一个得分/转牌表并且这样做我需要选择枢轴模型.
>不能做类似$events-> player-> first() – >得分()或事件$events-> player-> first() – > pivot-> scores(),它突破了以下内容:
PHP error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model
2)当Party扩展模型时:
>可以做一些琐碎的事情,比如Party :: count(),Party :: where(“Game_id”,$game-> id)
>失去所有的枢轴功能,所以不能做$game->玩家,甚至不是$game-> player()工作:$game-> players() – > count()是正确的,但$game – > players() – > first()或$game-> players-> first()将突破以下内容:
PHP error: Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array,object given
题:
如何在实例化时正确使用自定义枢轴模型是不同的?
我希望实现以下目标:
>做正常的$events->球员 – >附加($player)
>能够附加分数,如$score-> party_id = $events-> players-> find($x) – > pivot-> id(或通过范围的任何其他方式)
>能够计算正在玩的所有方,或某个特定游戏或特定玩家的方(如Party :: count(),$player-> parties-> count()等)
谢谢.
解决方法
由于您可以成像,因此用户A只需要与角色B建立一个链接/关系.
但是,如果我们想要使用需要在userA和productB之间保留不同记录的数据透视表(例如Sales),会发生什么?我没有找到直接的方法来做到这一点.
我的解决方案是将intermadiate表视为一个实际模型,然后与两个表(用户 – 销售和产品 – 销售)建立一对多的关系,然后,如果我需要,将这个中间模型与另一个模型联系起来.
所以在你的情况下,我有4个明确的模型:
>播放器|与党一对多
>游戏|与党一对多
>派对|与分数一对一|与党多对一|与玩家多对一
>得分|与党一对一