-2

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('/');   
}
//----------------------

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Isma'el
  • 23
  • 6
  • 1
    [codereview](https://codereview.stackexchange.com) is the right place for this type of question. – Niklesh Raut Jul 19 '20 at 15:34
  • 1
    Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jul 19 '20 at 15:39
  • 1
    With Laravel, you should really use Models instead of the DB facade. – Qirel Jul 19 '20 at 15:40

2 Answers2

1
DB::table('categories')->where('id', $id)->increment('position', 1);
DB::table('categories')->where('id', $nid)->decrement('position', 1);

Details on https://laravel.com/docs/7.x/queries#increment-and-decrement

xNoJustice
  • 516
  • 5
  • 8
0

The problem was solved this way. The solution was not as hard as beatifying this code.

 public function catup(Request $request, $id){
 $po = cats::select('position')->where('id', $id)->first();
 $thepostion = $po->position;
 cats::where('id', $id)->decrement('position', 1);   

 cats::where('position','<' ,$thepostion)
->orderby('position', 'desc')->first()->increment('position', 1);
 return redirect('/'); }

  
public function catdown(Request $request, $id){
  $po = cats::select('position')->where('id', $id)->first(); 
  $thepostion = $po->position;
 cats::where('id', $id)->increment('position', 1);
 
cats::where('position','>' ,$thepostion)
->orderby('position', 'desc')->first()->decrement('position', 1);
      return redirect('/');
    }
Isma'el
  • 23
  • 6