where('checkoutable_type', 'App\Models\Asset') ->whereHas('checkoutable', function($query) { $query->where('archived', 0); }) ->with(['assignedTo', 'checkoutable.assignedTo', 'checkoutable.model', 'checkoutable.adminuser']) ->get(); $count = 0; $unacceptedAssets = $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($unacceptedAssets as $unacceptedAsset) { if ($unacceptedAsset['acceptance']->assignedTo->email == ''){ $no_mail_address[] = $unacceptedAsset['checkoutable']->assignedTo->present()->fullName; } if ($unacceptedAsset['acceptance']->assignedTo) { if (!$unacceptedAsset['acceptance']->assignedTo->locale) { Notification::locale(Setting::getSettings()->locale)->send( $unacceptedAsset['acceptance']->assignedTo, new CheckoutAssetNotification($unacceptedAsset['assetItem'], $unacceptedAsset['acceptance']->assignedTo, $unacceptedAsset['assetItem']->adminuser, $unacceptedAsset['acceptance'], '') ); } else { Notification::send( $unacceptedAsset['acceptance']->assignedTo, new CheckoutAssetNotification($unacceptedAsset['assetItem'], $unacceptedAsset['acceptance']->assignedTo, $unacceptedAsset['assetItem']->adminuser, $unacceptedAsset['acceptance'], '') ); } $count++; } } if (!empty($no_mail_address)) { foreach($no_mail_address as $user) { return $user.' has no email.'; } } $this->info($count.' users notified.'); } }