4

How can I run this SQL statement with Laravel Eloquent ORM using my model?

update products set quantity = quantity + 3
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Shynkk
  • 43
  • 4
  • 1
    Take note of [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) – danblack Apr 11 '19 at 23:22
  • Read the docs, please: https://laravel.com/docs/5.8/database#running-queries – ceejayoz Apr 12 '19 at 01:30

1 Answers1

5

First you need to get specific instace to change:

$product = Product::find($id);

Now, you have all attributes of object and can change loaded values:

$product->quantity += 3;

Finally, you must to save changes:

$product->save();

Then, if you check your database the value will be changed.

Ps.: using Eloquent.

  • 4
    It's probably better to use `$product->increment('quantity', 3)` here. https://laravel.com/docs/5.8/queries#increment-and-decrement – ceejayoz Apr 12 '19 at 01:30