13

What I am trying to do is to update or insert the row in table. In my case, update looks something like this:

\DB::table('inventories')->where('product_code',$product_code)->increment('stock_current_quantity',$quantity);

I don't want to use if else statement. What I actually want is to integrate increment into following statement so it update as above statement.

 \App\Inventory::updateOrCreate(['product_code' => $product_code], ['stock_current_quantity'=>$quantity]);

Thanks in advance!

1
  • Why you are not using the Eloquent? It's so easy with Eloquent Model. Commented Oct 17, 2015 at 7:26

6 Answers 6

26

Because google brought me here and I think the answers here, especially when you simply want to use updateOrCreate, are not as satisfying as this:

\App\Inventory::updateOrCreate([
         'product_code' => $product_code
    ], 
    [
         'stock_current_quantity' => \DB::raw('stock_current_quantity + 1')
    ]
);

Credits to this guy

Sign up to request clarification or add additional context in comments.

2 Comments

if you use these code ,you can get an error like this: Eloquent update with DB::raw triggers Object of class Illuminate/Database/Query/Expression could not be converted to int
It doesn't work at create (actually INSERT) because the field stock_current_quantity is not yet set.
16

I am using Laravel 7.8.1 and rolling it all into one statement as per the following works fine:

    $user->league_entries()->updateOrCreate([
        'month' => $month,
        'year' => $year
    ])->increment('score');

2 Comments

Can you get the model id from this query ?
I believe the statement above just returns a true value.
14

Why not do:

$inventory = \App\Inventory::firstOrNew(['product_code' => $product_code]);

$inventory->stock_current_quantity = ($inventory->stock_current_quantity + $quantity);
$inventory->save();

If the model doesn't exists, $inventory->stock_current_quantity will only be equal to $quantity, else, it will increment it by $quantity.

3 Comments

There's a small risk that the database stock_current_quantity might change in between retrieving the item and saving it though. That's why increment is safer to use
Yup. There's that. If that's a concern and product_code is a unique index and MySQL is the only DBMS used, perhaps an insert ... on duplicate key update statement is more appropriate.
this may cause raise conditions. Its better to use tomstig's method
1

just contributing if someone else has this problem

it doesn't exist, creates, but with zero

self::firstOrNew(['product_code' => $product_code]);

then, increment it... if other process updates it to 1 before me (in this middle time between firstOrNew and increment), this will update to 2, avoiding losing the prior update

self::where('product_code', $product_code)->increment('stock_current_quantity');

Comments

1

Similar to Inigo's answer, but slight change i would go with:

 $user->league_entries()->firstOrCreate([
        'month' => $month,
        'year' => $year
    ])->increment('score');

Comments

0
Model::updateOrInsert([
 "name" => "last_run21",
 ], [
 "value" => DB::raw("IF(ISNULL(value + 1),1,value+1)"),
]);

This basically either creates the entry with name == "last_run21" with a initial value of 1 or updates it with value+1.

Tested on Mysql.

1 Comment

In order to use the Model directly, you need to adjust the namespace on the top.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.