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

时间:2016-09-19 11:51:43

标签: php mysql laravel eloquent

我需要为查询创建一个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();

1 个答案:

答案 0 :(得分:3)

试试这个:

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