public function changePosition($id, $new_index) { $home_banner = HomeBanner::find($id); $result = false; if ($home_banner) { $sorted_home_banners = HomeBanner::orderBy('position')->lists('id')->toArray(); $positionEntity = HomeBanner::where('id', $sorted_home_banners[$new_index])->first(); if ($positionEntity->position < $home_banner->position) { $home_banner->moveBefore($positionEntity); } else { $home_banner->moveAfter($positionEntity); } $result = $home_banner; } return $result; }
public function update($id) { $input = Input::all(); $rules = array('image' => 'image'); $niceNames = array('image' => 'Home Banner'); $validator = Validator::make($input, $rules); $validator->setAttributeNames($niceNames); if ($validator->fails()) { return Response::json(['success' => false, 'errors' => $validator->getMessageBag()->toArray()]); } else { $HomeBanner = HomeBanner::find($id); if (Input::has('show')) { $HomeBanner->show = Input::get('show'); } if (Input::has('ordering')) { $HomeBanner->ordering = Input::get('ordering'); } if (Input::has('status')) { $HomeBanner->status = Input::get('status'); } if (Input::has('link_path')) { $HomeBanner->link_path = Input::get('link_path'); } if (Input::has('title')) { $HomeBanner->title = Input::get('title'); } if (Input::hasfile('image')) { $file = Input::file('image'); $destinationPath = 'uploads/banners/'; $filename = $file->getClientOriginalName(); Input::file('image')->move($destinationPath, $filename); $ext = substr($filename, strrpos($filename, ".")); $newFileName = basename($filename, $ext) . "_" . $HomeBanner->id . "_" . date("Ymdhis") . $ext; rename($destinationPath . $filename, $destinationPath . $newFileName); HomeBanner::where('id', $HomeBanner->id)->update(['image_path' => $destinationPath . $newFileName]); } $HomeBanner->save(); if (Input::hasfile('image')) { return Response::json(['success' => true, 'message' => 'Home Banner has been updated!', 'file' => asset($destinationPath . $newFileName)]); } else { return Response::json(['success' => true, 'message' => 'Home Banner has been updated!']); } } }