public static function conditionPossibleMentor(Builder $builder, Model|self $user): Builder {
return $builder
->leftJoin($builder->raw('`users` mentors'), 'mentors.id', 'users.mentor_id')
->whereNot(
$user->getTable() . '.' . $user->getKeyName(),
$user->getKey()
)
->where(fn(Builder $query) => $query
->orWhereNull('users.mentor_id')
->orWhere('users.mentor_id', $user->mentor_id)
->orWhere(fn(Builder $query) => $query
->whereNot('users.mentor_id', $user->getKey())
->whereNot('mentors.mentor_id', $user->getKey())
)
)
;
}
public static function statusOperational(Builder $builder): Builder {
return static::builderJoinStatus($builder)
->where(function (Builder $query) {
$query
->orWhere('user_statuses.is_operational', true)
->orWhereNull('user_statuses.is_operational');
})
;
}
public static function builderJoinStatus(Builder $builder, string $join_type = 'left'): Builder {
static::builderSelectUsersAll($builder);
$join = $join_type . 'Join';
if (!array_reduce(
$builder->toBase()->joins ?? [],
fn(bool $carry, JoinClause $clause) => $carry |= $clause->table == 'user_statuses',
false
)) {
$builder->{$join}('user_statuses', 'users.user_status_name', 'user_statuses.name');
}
return $builder;
}