0

does laravel have short cut for $i++; i want to update my database value went seomething happend. on php i could just use sql like

UPDATE goods SET qty++ WHERE id = '4' 

here is my controller code

public function store(Request $request)
{
      $g = new goods();
      $g->qty = ++;
      $g->save();
}
Ritul Lakhtariya
  • 362
  • 1
  • 16
Martin Christopher
  • 389
  • 1
  • 7
  • 21

4 Answers4

1

Here is the solution

$post      = Goods::find(3);
$post->qty = $post->qty + 1;
$post->save();

you can just find that record and update with +1.

Ritul Lakhtariya
  • 362
  • 1
  • 16
1

Look at the docs: https://laravel.com/docs/5.7/queries#increment-and-decrement For example: DB::table('users')->increment('votes', 5);

Ruub
  • 619
  • 1
  • 13
  • 41
0

Can you try this.

public function store(Request $request) {
    $g = goods::find(4);
    $g->qty += 1;
    $g->save;
}
Kiran Kanzar
  • 329
  • 1
  • 5
  • 14
0

You can't just call ++ on nothing, and doing (if it would work) would just add 1. Why not just say $g->qty = 1;? If you already have some value in qty, then call $g->qty++; and then call ->save(); on it (note the "()").

Christian Kaal
  • 232
  • 3
  • 13