예제 #1
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $this->validate($request, ['name' => 'required|string|not_in:<script>|max:255', 'order' => 'required|integer']);
     $category = Channel::find($id);
     $orderOld = $category->order;
     $orderNew = $request->input('order');
     $orderGap = $orderOld - $orderNew;
     $orderGen = 0;
     if ($request->input('name') && $category) {
         $category->update($request->all());
     }
     if ($orderGap < 0) {
         $orderGen = -1;
         $categories = Channel::whereBetween('order', [$orderOld, $orderNew])->where('id', '!=', $category->id)->get();
         foreach ($categories as $catg) {
             $catg->update(['order' => $catg->order + $orderGen]);
         }
     } elseif ($orderGap > 0) {
         $orderGen = 1;
         $categories = Channel::whereBetween('order', [$orderNew, $orderOld])->where('id', '!=', $category->id)->get();
         foreach ($categories as $catg) {
             $catg->update(['order' => $catg->order + $orderGen]);
         }
     }
     return redirect()->action('Admin\\CatgWebboardController@index');
 }