数据透视表中的位置

时间:2018-01-18 09:18:11

标签: php laravel pivot where-clause

我有下一个数据透视表:

Schema::create('coach_user', function(Blueprint $table)
        {
            $table->integer('coach_id')->unsigned()->index();
            $table->foreign('coach_id')->references('id')->on('coaches')->onDelete('cascade');
            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->tinyInteger('rank');
        });

在User.php中:

 public function coaches()
    {
        return $this->belongsToMany(\App\Coach::class)->withPivot('rank');
    }

我如何接受一些排名的用户教练?像这样:

$user->coaches->where('rank',1)->get().

2 个答案:

答案 0 :(得分:2)

使用wherePivot() filter belongsToMany $user->coaches()->wherePivot('rank',1)->get(); 返回的结果。

self.view.insertSubview(VC1.view, belowSubview: thatview)

答案 1 :(得分:1)

wherePivot用于数据透视列和关系作为方法:

$user->coaches()->wherePivot('rank',1)->get().