public function postUpdateMediasPosition(Request $request) { $itemId = intVal($request->input('id')); $mediaId = intVal($request->input('mediaId')); $item = call_user_func(array($this->class_name, 'findOrFail'), $itemId); $media = Media::findOrFail($mediaId); $collection = $media->collection; $medias = $item->getMedias($collection); $oldPos = $media->position; $newPos = intVal($request->input('position')); if ($oldPos != $newPos) { if ($oldPos < $newPos) { $range = [$oldPos + 1, $newPos]; } else { $range = [$newPos, $oldPos - 1]; } // whereBetween does not exist for collections as Laravel 5.2! //$medias = $medias->whereBetween('position', $range); $rangeList = []; for ($i = $range[0]; $i <= $range[1]; ++$i) { $rangeList[] = $i; } $medias = $medias->whereIn('position', $rangeList); // increment and devrement does not exist for collections as Laravel 5.2! if ($oldPos < $newPos) { foreach ($medias as &$m) { --$m->position; $m->save(); } } else { foreach ($medias as &$m) { ++$m->position; $m->save(); } } $media->position = $newPos; $media->save(); } }
public function update(Request $request) { $id = $request->input('id', 0); $media = Media::findOrFail($id); $media->title = $request->input('title', 'tmp_name'); $media->save(); }