public function create(Request $request) { $schools = $this->school->lists('school_name', 'id')->all(); $all_contacts = $this->contact->allStudents(); $contacts = array(); foreach ($all_contacts as $contact) { $contacts[$contact->id] = $contact->name . ' ' . $contact->surname; } $students = new Collection(); if ($request->input('message_id')) { $messages = Message::processIds($request->input('message_id')); foreach ($messages as $message) { $students->add($message->Contact); } $students = $students->unique(); } if ($students->count()) { $selected = array(); foreach ($students as $student) { $selected[] = $student->id; } $second_email = false; $student_emails = ""; foreach ($students as $student) { if ($second_email) { $student_emails .= ', ' . $student->user_email; } else { $student_emails .= $student->user_email; $second_email = true; } } } else { $selected = null; $student_emails = null; } /* if ($request->input('student_checkbox')) { $selected = array(); foreach ($request->input('student_checkbox') as $student_id => $foo) { $selected[] = $student_id; } $students = $this->contact->whereIn('id', $selected)->get(); $second_email = false; $student_emails = ""; foreach ($students as $student) { if ($second_email) { $student_emails .= ', '.$student->user_email; } else { $student_emails .= $student->user_email; $second_email = true; } } } else { $selected = null; $student_emails = null; } */ return view()->make('emails.create', compact('contacts', 'schools', 'selected', 'student_emails')); }
public function exportCSV(Request $request) { //$messages = $this->message->where('hidden', '0')->get(); if ($request->input('message_id')) { $messages = Message::processIds($request->input('message_id')); if ($messages->count()) { $destinationPath = 'downloads/' . time() . '-' . str_random(8); $filename = 'export-' . str_random(8) . '.csv'; $directory = File::makeDirectory($destinationPath, 0770); $file = fopen($destinationPath . '/' . $filename, 'w'); fputcsv($file, array('id', 'date', 'student', 'type', 'school', 'notes')); foreach ($messages as $message) { if (count($message->School) > 0) { $school = $message->School->school_name; } else { $school = null; } fputcsv($file, array($message->id, $message->updated_at, $message->Contact->fullName(), $message->MessageType->message_type_name, $school, $message->contents)); } $file = fclose($file); $headers = array('Content-Type' => 'text/csv'); return response()->download($destinationPath . '/' . $filename, $filename, $headers); } } else { return redirect()->back()->withError('Please select some records'); } }