在数据透视表中的位置

Where in pivot tables

我有下一个支点table:

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().

使用 wherePivot()filter belongsToMany 返回的结果。

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

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

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