/** * @param $id * @param AdminFormRequest $request * @return \Illuminate\Http\RedirectResponse */ public function patchUpdate($id, AdminFormRequest $request) { $user = User::findOrFail($id); $file = $request->file('image'); $msg = 'Update user failed'; if (!empty($file)) { //deleting old file $objPhoto = Photo::find($user->photo_id); $photoName = base_path() . $this->storagePath . $objPhoto->photo_name; @unlink($photoName); $objPhoto->delete(); //writing new file $extName = $file->getClientOriginalExtension(); //Generating image name $imageName = uniqid(rand()) . ".{$extName}"; $image = new Photo(); $image->photo_name = $imageName; $image->save(); //moving file $fullPath = base_path() . $this->storagePath; $file->move($fullPath, $imageName); //save all with relations if ($image->users()->save($user)) { $msg = 'User was update'; } } else { $user->update($request->all()); if ($user->save()) { $msg = 'User was updated'; } } return redirect()->action('AdminController@getIndex')->with('message', $msg); }