where('checkoutable_type', 'App\Models\Asset') ->whereHas('checkoutable', function($query) { $query->where('accepted_at', null) ->where('declined_at', null); }) ->with(['assignedTo', 'checkoutable.assignedTo', 'checkoutable.model', 'checkoutable.adminuser']) ->get(); $count = 0; $unacceptedAssetGroups = $pending ->filter(function($acceptance) { return $acceptance->checkoutable_type == 'App\Models\Asset'; }) ->map(function($acceptance) { return ['assetItem' => $acceptance->checkoutable, 'acceptance' => $acceptance]; }) ->groupBy(function($item) { return $item['acceptance']->assignedTo ? $item['acceptance']->assignedTo->id : ''; }); $no_mail_address = []; foreach($unacceptedAssetGroups as $unacceptedAssetGroup) { $locale = $unacceptedAssetGroup[0]['acceptance']->assignedTo?->locale; $email = $unacceptedAssetGroup[0]['acceptance']->assignedTo?->email; $item_count = $unacceptedAssetGroup->count(); if ($locale && $email) { Mail::to($email)->send((new UnacceptedAssetReminderMail($unacceptedAssetGroup[0]['acceptance'], $item_count))->locale($locale)); } elseif ($email) { Mail::to($email)->send((new UnacceptedAssetReminderMail($unacceptedAssetGroup[0]['acceptance'], $item_count))); } $count++; } if (!empty($no_mail_address)) { foreach($no_mail_address as $user) { return $user.' has no email.'; } } $this->info($count.' users notified.'); } }