public function postAddBook() { if (\Auth::check()) { $data = Input::all(); array_pop($data); $rules = ['book_id' => 'required', 'book_name' => 'required', 'book_desc' => 'required', 'mail' => 'required']; $validator = Validator::make($data, $rules); if ($validator->fails()) { return Redirect::back()->withErrors($validator->errors())->withInput(); } else { $arrival = new Arrivals(); $arrival->book_id = $data['book_id']; $arrival->book_name = $data['book_name']; $arrival->book_desc = $data['book_desc']; $arrival->save(); if ($data['mail'] == 0) { $staffs = Staff::Orderby('id', 'des')->get(); foreach ($staffs as $staff) { Mail::send('mail.mail', array('name' => $staff->name, 'book_name' => $data['book_name']), function ($message) use($staff) { $message->to($staff->email, $staff->name)->subject('Library Notification'); }); } } elseif ($data['mail'] == 1) { $students = Student::Orderby('id', 'des')->get(); foreach ($students as $student) { Mail::send('mail.mail', array('name' => $student->name, 'book_name' => $data['book_name']), function ($message) use($student) { $message->to($student->email, $student->name)->subject('Library Notification'); }); } } else { $staffs = Staff::Orderby('id', 'des')->get(); foreach ($staffs as $staff) { Mail::send('mail.mail', array('name' => $staff->name, 'book_name' => $data['book_name']), function ($message) use($staff) { $message->to($staff->email, $staff->name)->subject('Library Notification'); }); } $students = Student::Orderby('id', 'des')->get(); foreach ($students as $student) { Mail::send('mail.mail', array('name' => $student->name, 'book_name' => $data['book_name']), function ($message) use($student) { $message->to($student->email, $student->name)->subject('Library Notification'); }); } } Session::flash('err', "1"); return redirect('add_books'); } } else { return redirect('login'); } }