next($schedulingService->getPickupDay()->value); $employees = $this->getEmployeesToNotify($pickupDay); foreach ($employees as $employee) { $employee->notify(new MissingOrderReminderNotification($pickupDay)); } } /** @return Collection */ private function getEmployeesToNotify(CarbonInterface $pickupDay): Collection { $excludedMatricules = Order::query() ->whereDate('pickup_day', $pickupDay->format('Y-m-d')) ->where('status', OrderStatus::VALIDATED) ->get() ->map(fn (Order $order): Matricule => $order->employee->matricule) ->unique(); return Employee::query() ->with('customer') ->whereNotIn('matricule', $excludedMatricules) ->get() ->filter(fn (Employee $employee): bool => $this->canNotifyEmployee($employee)); } private function canNotifyEmployee(Employee $employee): bool { $customer = $employee->customer; return $customer && Gate::forUser($customer)->check('customerPassOrders'); } }