I am new to laravel ..
I am in need to update the position field in the categories table for two records each time i.e when a user clicks an up arrow in the interface the action should be to decrease this record -1 and to increase the previous +1 AND if a down arrow was clicked the action will be to increase +1 the position field value for this record and decrease the next one.
I hope I explained the case with my bad English. have a look at this code and see if am using the right approach.
public function catup(Request $request, $id)
{
$previous = DB::table('categories')->where('id','<' ,$id)->orderby('id', 'desc')->first();
$pid = $previous->id;
DB::table('categories')->where('id', $id)->update([ 'position' => DB::raw('position - 1')]);
DB::table('categories')->where('id',$pid)->update([ 'position' => DB::raw('position + 1')]);
return redirect('/');
}
//--------------------------
public function catdown(Request $request, $id){
$next = DB::table('categories')->where('id','>' ,$id)->orderby('id', 'asc')->first();
$nid=$next->id;
DB::table('categories')->where('id', $nid)->update([ 'position' => DB::raw('position - 1')]);
DB::table('categories')->where('id', $id)->update([ 'position' => DB::raw('position + 1')]);
return redirect('/');
}
//----------------------