从关系中查询数据透视表中的列值


Laravel - query for column value in pivot table from relationship

我需要为查询做一个if语句,我需要检查列status是否被设置为rematch,以便在数据透视表player_quiz中进行测试。以下是quiz模型中建立关系的方法:

public function players()
    {
        return $this->belongsToMany('App'Player', 'player_quiz')->withPivot('status');
    }

我不确定如何做这种查询,让它更清楚这里它应该基本上看起来像:

if ($quiz->players()->where('status', 'rematch'))->get();

试试这个:

if ($quiz->players()->wherePivot('status', 'rematch')->get()) {
   // Do something
}