After various search I found out that the solution was quite easy: it's enough to use the query
method
->query(function (Builder $query, array $data) {
if (blank($data['value'])) {
return $query;
}
return $query->whereHas('player', function (Builder $query) use ($data) {
$query->where('sex', $data['value']);
});
})