utc(); $userFlights = UserFlight::where('departure_date', '<=', $now->toDateTimeString()) ->where(function ($query) { $query->whereNull('departure_processed_at') ->orWhereColumn('departure_processed_at', '<', 'departure_date'); }) ->get(); foreach ($userFlights as $flight) { $flight->update(['departure_processed_at' => $now]); $this->notifyFollowersOnDeparture($flight); } } protected function notifyFollowersOnDeparture(UserFlight $flight): void { $user = $flight->user; $title = "{$user->name} is taking off!"; $flightNumber = $flight->flight_number ? "On {$flight->flight_number} from" : 'From'; $body = "{$flightNumber} {$flight->departureAirport->municipality} → {$flight->arrivalAirport->municipality}"; $url = route('profile.flight', [ 'user' => $user->id, 'userFlight' => $flight->id, ]); $user->followers()->get()->each(function (User $follower) use ($title, $body, $url) { $notify = $follower->getSetting('notify_on_flight_departure'); if ($notify) { Notification::create([ 'user_id' => $follower->id, 'title' => $title, 'body' => $body, 'url' => $url, ]); } }); } }