我有下一个数据透视表:
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().
答案 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().