public function exportExcel() { if (Gate::denies('download', Guest::class)) { abort(403); } $records = Guest::select([\DB::raw("CONCAT(guests.last_name, ', ', guests.first_name) as 'display_name'"), 'first_name', 'middle_name', 'last_name', 'gender', 'age', 'civil_status', 'education', 'occupation', 'agency', 'role', 'email', 'contact_number', 'photo_url']); $records = $records->orderBy('display_name', 'asc')->get(); $recordsArray = []; $recordsArray[] = ['display_name', 'first_name', 'middle_name', 'last_name', 'gender', 'age', 'civil_status', 'education', 'occupation', 'agency', 'role', 'email', 'contact_number', 'photo_url']; foreach ($records as $record) { $recordsArray[] = $record->toArray(); } $now = Carbon::now('Asia/Manila'); $file_name = 'arp_guests_' . $now->format('Ymd_His'); Excel::create($file_name, function ($excel) use($recordsArray) { $excel->setTitle('ARP Guests'); $excel->setDescription('List of ARP Guests'); $excel->sheet('Guests', function ($sheet) use($recordsArray) { $sheet->fromArray($recordsArray, null, 'A1', false, false); }); })->download('xlsx'); }