SHARE
TWEET

Untitled

a guest May 19th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function postSortFields(Request $request)
  2.     {
  3.  
  4.         $req = $request->all();
  5.         $data = Sort::orderBy('order', 'asc')->get();
  6.  
  7.         $sort = Sort::find($req['fieldId']);
  8.         if ($req['order'] > $sort->order) {
  9.             $sort->update(['order' => $req['order']]);
  10.             foreach ($data as $key => $value) {
  11.                 if ($value->order <= $req['order'] && $sort->id != $value->id) {
  12.                     $order = $key - 1 ;
  13.                     if ($value->order == $req['order']) {
  14.                         $order = $req['order'] - 1;
  15.                     }
  16.  
  17.                     $value->order = $order;
  18.                 } elseif ($value->order > $req['order']) {
  19.                     $value->order = $key;
  20.                 }
  21.                 $value->save();
  22.             }
  23.         } else {
  24.  
  25.             $sort->update(['order' => $req['order']]);
  26.  
  27.             foreach ($data as $key => $value) {
  28.                 if ($value->order >= $req['order'] && $value->id != $sort->id) {
  29.                     $value->order = $key + 1;
  30.                     $value->save();
  31.                 }
  32.             }
  33.         }
  34.  
  35.         return $this->success(Sort::orderBy('order', 'asc')->get());
  36.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top