Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nanadjei2's avatar

Php does not store negative values.

I attempted to do an update with a negative value but it never worked. No error was returned as well. For example:

    $prod = App\Models\Product::find(1)
    $prod->update(['quantity_remaining' => -1])
0 likes
27 replies
OussamaMater's avatar

Well you need to make sure that quantity_remaining column is SIGNED otherwise it will only store zero and positive numbers, so check what you have in your migration and update it.

1 like
nanadjei2's avatar

@OussamaMater This is my migration $table->bigInteger('quantity_remaining'); I also decided to add ->signed() method to it and it still don't work

OussamaMater's avatar

@nanadjei2 have you re-run the migrations when you added the signed() because it won't be affected by just appending the signed() and what I would do is run a sql command desc <table> and check the type, to see if that column is signed or not, because in your case if it starts from zero only it's unsigned().

1 like
nanadjei2's avatar

@OussamaMater Yes I re-run my migration before testing after I included the signed method. I just checked the structure of my database and the column is signed. The funny thing is, if I hardcore a negative figure in the quantity remaining column it works. I tried tinkering an update, it got stacked for a while and nothing was returned. I went on to query again to see of the update was successful but the quantity_remaining was still 0.

OussamaMater's avatar

@nanadjei2 Yes that's a bit confusing to be honest, but if you hard coded the value and it worked, but when in the logic didn't, there should be a bug somewhere else in the code.

1 like
tykus's avatar

@nanadjei2 is the quantity_remaining property $fillable on the Model? Does it work if your code is like this:

$prod = App\Models\Product::find(1);
$prod->quantity_remaining = -1;
$prod->save();
Tray2's avatar

My guess is that you have a validation rule that does not allow negative values, and I agree with that rule, a remaining quantity should not be allowed to be negative.

OussamaMater's avatar

@Tray2 In his case I don't really think it's a validation rule (based on the example), he hard coded the value to test if it works or not, but still didn't.

1 like
nanadjei2's avatar

@Tray2 Thank you for your submission. I agree with that. That's how the 1st version of the app was written. The remaining quality cannot be negative. But in a typical use case, there are instances where products can be in stock or may be present in our inventory but may be out of stock in the app due to human errors or some other reason. In that case the quantity remaining shoud to be updated to reconcile with the one in stock at every instance. Hence, I decided to allow negation so that when I decide to update the product with the right figure, the negation can happen automatically no matter the number of times it has been negated. For example: If a product has -2 as it's quantity remaining and I update it to 5, we can have 3 as the quantity remaining. I hope my use case makes sense.

Tray2's avatar

@nanadjei2 It does not, the stock should become 5 and then if you have open orders they should then claim 2 from the stock and thus make it 3.

click's avatar

And this does work if you run it exactly the same as your -1 approach?

$prod->update(['quantity_remaining' => 99])
1 like
RayC's avatar

Try decrementing the value using

--$prod->quantity_remaining;
// Or
$prod->quantity_remaining -= 1;
tykus's avatar

@RayC all this does is change the value on the instance; it does nothing to persist the value in the database. And

  • —$prod->quantity_remaining negates the existing value
  • $prod->quantity_remaining -= 1 decrements the value; it will not result in -1 (unless the current value is 0)
RayC's avatar

@tykus Yes it changes the value, was for the OP to test.

  • --$prod->quantity_remaining; was what I had posted, it added the two dashes together, which with two preceeding -- dashes it would decrement the value. Fixed in the previous post.
  • Correct, however if null will have not effect. Assuming they are using 0 as an initial value it will decrement it by one to make it a negative -1.
nanadjei2's avatar

I am now getting this error Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) And I have extended my memory limit to 2048M but I am still getting the error

nanadjei2's avatar

@tykus No. Now, updating does not work even if I want to change the name of the product.

Sinnbeck's avatar

@nanadjei2 this does not sound to be related to -1? If you set it to 1 the error is gone?

And still waiting for you to show the model

2 likes
nanadjei2's avatar

@Sinnbeck I had a model event in my Model that was preventing my update from working...

public static function boot()
   {
       parent::boot();
       static::updating(function ($product) {
           if ($product->quantity_remaining <= 0) {
               return $product->update(['in_stock' => false]);
           }
           return $product->update(['in_stock' => true]);
       });
   }
OussamaMater's avatar

@nanadjei2 I followed this thread just to see what went wrong XD and it was just a model event XD

Well I guess this closes up the thread.

Sinnbeck's avatar

@nanadjei2 Well there we go. This is why it is a good idea to show what we ask for. Had you shown it 16 hours ago, it would have been solved :)

1 like
nanadjei2's avatar

I still don't get why it doesn't work with this model event.

tykus's avatar
tykus
Best Answer
Level 104

@nanadjei2 you are trying to update inside the updating model event - each update triggers a new updating event. instead, you should simply assign to properties on the Model:

public static function boot()
{
       parent::boot();
       static::updating(function ($product) {
           $product->in_stock = $product->quantity_remaining >= 0;
       });
}

This will modify the in_stock property on the Model right before the database is written.

1 like
nanadjei2's avatar

@tykus Oh My God. I just copied my controller method into the boot method and started testing 😄.

Please or to participate in this conversation.