copy()->startOfWeek(Carbon::SUNDAY); $weekEnd = $date->copy()->endOfWeek(Carbon::SATURDAY); $weeklySchedulesCount = Schedule::where('client_id', $clientId) ->where('provider_id', $providerId) ->whereNotIn('status', self::EXCLUDED_STATUSES) ->whereBetween('date', [$weekStart->format('Y-m-d'), $weekEnd->format('Y-m-d')]) ->when($excludeScheduleId, function ($query) use ($excludeScheduleId) { $query->where('id', '!=', $excludeScheduleId); }) ->count(); if ($weeklySchedulesCount >= 2) { throw new \Exception(__('validation.custom.schedule.weekly_limit_exceeded')); } return true; } }