【问题标题】:Laravel relation from a pivot来自枢轴的Laravel关系
【发布时间】:2020-12-04 05:24:39
【问题描述】:

我有一个关系,我无法弄清楚我当前的应用程序看起来像这样

模型

User.php
    public function teams()
    {
        return $this->belongsToMany(Team::class);
    }
Team.php

    public function tournaments()
    {
        return $this->belongsToMany(Tournament::class);
    }
Tournament.php

    public function teams()
    {
        return $this->belongsToMany(Team::class);
    }

表格

tournaments
id
teams
id
team_tournament
tournament_id
team_id

现在锦标赛可以只有部分用户参加锦标赛,所以我创建了以下内容

team_tournament_users
tournament_id
team_id
user_id

存储团队中参加锦标赛的用户。

这应该如何看待雄辩的关系?

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    所以带着你的 team_tournament_users,我会做以下事情

    将使用锦标赛的命名约定 participants,因此桌名将是 participants 而不是 team_tournament_users

    迁移

    <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateParticipantsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('participants', function (Blueprint $table) {
                $table->foreignId('tournament_id')->constrained();
                $table->foreignId('user_id')->constrained();
                $table->foreignId('team_id')->constrained();
                $table->timestamps();
    
                $table->primary(['tournament_id', 'user_id']);
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('participants');
        }
    }
    

    关系:然后会在模型中定义以下关系

    class User extends Model
    {
    
        public function teams()
        {
            return $this->belongsToMany(Team::class);
        }
    
        public function tournaments()
        {
            return $this->belongsToMany(Tournament::class, 'participants')
                ->withPivot('team_id')
                ->withTimestamps();
        }
    }
    
    
    class Tournament extends Model
    {
    
        public function teams()
        {
            return $this->belongsToMany(Team::class);
        }
    
        public function participants()
        {
            return $this->belongsToMany(User::class, 'participants')
                ->withPivot('team_id')
                ->withTimestamps();
        }
    }
    
    class Team extends Model
    {
    
        public function users()
        {
            return $this->belongsToMany(User::class)->withTimestamps();
        }
    
        public function tournaments()
        {
            return $this->belongsToMany(Tournament::class)->withTimestamps();
        }
    }
    

    然后我将如何检索所有参与特定锦标赛的团队的用户?因此,如果从一个团队中选择了 3 位用户/成员参加比赛,我如何轻松找出该团队中的哪些成员被选中?

    //There's already a relation between Tournament and Team(s)
    //So we can use it to get the tournament participants from the team
    
    $t = Tournament::with('participants')->first()
      ->participants
      ->groupBy(function($participant){
        return $participant->pivot->team_id;    
      });
    
    //If Team with an id of 5 has members participating in the tournament
    //Then we can get the members of Team as
    $team = Team::findOrFail(5);
    $t->get($team->id);
    

    【讨论】:

    • 然后我将如何检索参与特定锦标赛的团队的所有用户?因此,如果从一个团队中选择了 3 名用户/成员参加比赛,我如何轻松找出该团队中的哪些成员被选中?
    • @Paradigm 已根据您的上述评论更新了我的答案
    • 谢谢您的答案已标记为正确,感谢您的帮助。
    猜你喜欢
    • 2019-07-05
    • 1970-01-01
    • 2018-05-16
    • 2019-07-27
    • 2020-08-23
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    相关资源
    最近更新 更多