first(); if($service){ $serviceName = $service->name; } else { $serviceName = '-'; } $data['service_name'] = $serviceName; // Map Histories to Group by Dates $historiesGroupByDate = $this->histories->mapToGroups(function($history) { return [$history->created_at->format('Y-m-d') => $history]; }); $data['histories_by_date'] = []; foreach ($historiesGroupByDate as $date => $histories) { $data['histories_by_date'][] = [ 'date' => $date, 'histories' => $histories ]; } // Map Files by type $filesGroupByType = $this->files->mapToGroups(function($file) { return [Str::slug($file->type, '_') => $file]; }); $data['files_by_type'] = $filesGroupByType; return $data; } }