예제 #1
0
 /**
  * Update the specified resource in storage.
  *
  * @param  Request  $request
  * @param  int  $id
  * @return Response
  */
 public function update(Request $request, $id)
 {
     $validator = $this->student->validate($request);
     if ($validator->fails()) {
         $student = json_decode(json_encode($request->all()), FALSE);
         $html = view('students._form', compact('student'))->withErrors($validator);
         return response()->json(['status' => 0, 'html' => $html->render()]);
     }
     $this->student->findOrFail($id)->update($request->except('student_code'));
     return response()->json(['status' => 1, 'message' => 'success']);
 }
 public function update($id, Request $request)
 {
     //------Validation-------
     $validator = Student::validate();
     if ($validator->fails()) {
         return redirect()->route('students.edit', array('id' => $id))->withErrors($validator)->withInput();
     }
     //-----End Validation-----
     try {
         $student = Student::findOrFail($id);
     } catch (Exception $e) {
         App::abort(404);
         //if student details not found in the database
     }
     $student->fill($request->input());
     //Display flash msg on successful updation
     if ($student->save()) {
         if (is_null($request->input("interests"))) {
             //no interests then remove from pivot table, (by passing an empty array)
             $student->interests()->sync(array());
         } else {
             //if saving of student is successful and interests is not null sync interests
             $student->interests()->sync($request->input("interests"));
         }
         Session::flash('message', "Student '" . $student->name . "' updated successfully. ");
         return redirect()->route('students.index');
     } else {
         Session::flash('message', "Updating student unsuccessful.");
         return redirect()->route('students.index');
     }
 }