首页 文章

Laravel自定义枢轴使用(扩展模型或扩展枢轴)

提问于
浏览
4

我真的不习惯使用我的支点的自定义模型,并且不明白为什么我们可以为它制作自定义模型,如果我们不能将它用作模型 . 我已经看过很多答案和文章,所有人都选择扩展Pivot或Model,但是现实生活中的使用从未被考虑过,而且从来没有比给它一个“命名”类更进一步 .

这是我想要实现的一个例子:我的主要模型: Player 模型,带有 players 表 . Game 型号,带 games 表 .

PlayerGame 有多对多的关系,我们可以称之为“ Party " with a " game_player ”表 .

现在不太重要,我还有一个持有得分/转弯的 Score 模型, Party 可以有很多分数,所以 scores 表有一个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, $table, $exists);
        }
        return parent::newPivot($parent, $attributes, $table, $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, array $attributes, $table, $exists) {
        if ($parent instanceof Player) {
            return new Party($parent, $attributes, $table, $exists);
        }
        return parent::newPivot($parent, $attributes, $table, $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时:

  • 不能像_1120957那样做任何与模型有关的事情 .

  • 不能做像 Party::where("player_id", $player->id) 之类的东西......基本上我希望派对上有一个得分/转弯表,为此我需要选择枢轴模型 .

  • 无法执行类似$ events-> player-> first() - > scores()或事件$ events-> player-> first() - > pivot-> scores(),它会突破以下内容:

PHP错误:传递给Illuminate \ Database \ Eloquent \ Relations \ Pivot :: __ construct()的参数1必须是Illuminate \ Database \ Eloquent \ Model的实例

2)当Party扩展模型时:

  • 可以做一些琐碎的事情,比如Party :: count(),Party :: where("Game_id",$ game-> id)

  • 失去所有的枢轴功能,所以不能做$ game->玩家,甚至不是$ game-> players()工作:$ game-> players() - > count()是正确的,但是$ game-> players( ) - > first()或$ game-> players-> first()将破坏以下内容:

PHP错误:参数1传递给Illuminate \ Database \ Eloquent \ Model :: __ construct()必须是类型数组,给定对象

题:

如何在实例化时正确使用自定义枢轴模型是不同的?

我希望实现以下目标:

  • 正常的$ events-> players-> attach($ player)

  • 能够附加分数,如$ score-> party_id = $ events-> players-> find($ x) - > pivot-> id(或通过范围的任何其他方式)

  • 能够统计正在玩的所有方,或特定游戏或特定玩家(如Party :: count(),$ player-> parties-> count()等)

谢谢 .

1 回答

  • 0

    对于我对laravel的一点体验,我发现 pivot tables 用于 many-to-many 关系,它只保留从表A到表B的特定对象之间的一个链接 . 例如User with Role .

    正如您可以想象的那样,用户A只需要与角色B Build 一个链接/关系 . 但是如果我们想要使用需要在userA和productB之间保留不同记录的数据透视表(例如Sales),会发生什么?我没有找到直接的方法来做到这一点 .

    我的解决方案是将intermadiate表视为一个实际模型,然后与两个表(用户 - 销售和产品 - 销售) Build 一对多的关系,然后,如果我需要,将这个中间模型与另一个模型联系起来 .

    所以在你的情况下,我有4个明确的模型:

    • Player | one-to-manyParty

    • Game | one-to-manyParty

    • Party | one-to-oneScore | many-to-oneParty | many-to-onePlayer

    • Score | one-to-oneParty

    使用这种方法,您还可以更好地控制每个模型的属性 .

相关问题