comment('Sending still running time entry emails...'); $dryRun = (bool) $this->option('dry-run'); if ($dryRun) { $this->comment('Running in dry-run mode. No emails will be sent and nothing will be saved to the database.'); } $sentMails = 0; TimeEntry::query() ->whereNull('end') ->where('start', '<', now()->subHours(8)) ->whereNull('still_active_email_sent_at') ->with([ 'user', ]) ->whereHas('user', function (Builder $query): void { /** @var Builder $query */ $query->where('is_placeholder', '=', false); }) ->orderBy('created_at', 'asc') ->chunk(500, function (Collection $timeEntries) use ($dryRun, &$sentMails): void { /** @var Collection $timeEntries */ foreach ($timeEntries as $timeEntry) { $user = $timeEntry->user; $this->info('Start sending email to user "'.$user->email.'" ('.$user->getKey().') for time entry '.$timeEntry->getKey()); $sentMails++; if (! $dryRun) { Mail::to($user->email) ->queue(new TimeEntryStillRunningMail($timeEntry, $user)); $timeEntry->still_active_email_sent_at = Carbon::now(); $timeEntry->save(); } } }); $this->comment('Finished sending '.$sentMails.' still running time entry emails...'); return self::SUCCESS; } }