info('Checking for expiring contracts...'); $expiringContracts = Contract::where('status', '!=', 'expired') // Assuming 'active' or 'pending' ->where('end_date', '<=', now()->addDays(30)) ->where('end_date', '>=', now()) ->get(); if ($expiringContracts->isEmpty()) { $this->info('No contracts expiring soon.'); return; } $admins = User::where('role', 'admin')->get(); if ($admins->isEmpty()) { $this->warn('No admins found to notify.'); return; } foreach ($admins as $admin) { // In a real app, you might group these or send one email per contract/batch // Here we just simulate sending a notification for the batch // Or send individual emails // Simplification: Send one email with list try { Mail::to($admin->email)->send(new ContractExpiringNotification($expiringContracts)); $this->info("Notification sent to {$admin->email}"); } catch (\Exception $e) { $this->error("Failed to send email to {$admin->email}: " . $e->getMessage()); Log::error($e); } } $this->info('Done.'); } }