when($captchaId, function (Builder $query, int $captchaId) { $query->where('captcha_id', $captchaId); }) ->where('type', '=', $type) ->count(); } public function getCaptchaLogsByTypes(array $types, ?int $captchaId = null, ?int $limit = null): Collection { return CaptchaLog::query() ->when($captchaId, function (Builder $query, int $captchaId) { $query->where('captcha_id', $captchaId); }) ->when($limit, function (Builder $query, int $limit) { $query->limit($limit); }) ->whereIn('type', $types) ->latest() ->get(); } }