Marco Marco - 2 months ago 11
MySQL Question

Laravel - query for column value in pivot table from relationship

I need to make an if statement for a query where I need to check if column

status
is set to
rematch
for players for that quiz in the pivot table
player_quiz
.
This is how to relationship is set up in the
quiz
model:

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


I am not sure how to do that kind of query, to make it clearer here how it should basically look like:

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

Answer

Try this one:

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