framework: Model::increment/Model::decrement does not fire Model Events

  • Laravel Version: 5.3
  • PHP Version: 7.1.1
  • Database Driver & Version:

Description:

Is there any specific reason why Model::increment/Model::decrement does not fire update events?

Steps To Reproduce:

Just increment/decrement anything on a model and try to catch eloquent.updating:*/eloquent.updated:*

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 22 (12 by maintainers)

Most upvoted comments

Why was this issue closed? This issue still exists.

Nobody is thinking this through. There is just complaining.

From my post on another issue:

Note that, sure, we can add the events to this method; however, there is no way to gurantee that the dirty / changed values are actually what the values are going to end up being in the database. We must always run this query as a query that looks like update table set column = column + 1 because multiple processes could be trying to increment the column at the same time.

Therefore, it is impossible for us to know what the new column value will actually be until after we run the query, so the “dirty” value in the updating event would always be a guesstimate. It could never be guaranteed to be correct.

Whether this matters to you depends on what you are doing in the event handler.

I think, decrement and increment, should fire update events! It’s updating the model.

Please reopen this issue.

I completely agree here.

Overriding the increment / decrement method works for me

Added this to my models in which I need the save event to trigger

<?php

namespace App\Traits;

trait IncrementDecrement
{
    public function increment($column, $amount = 1, array $extra = [])
    {
        $this->$column = $this->$column + $amount;
    
        $this->save();
    }
    
    public function decrement($column, $amount = 1, array $extra = [])
    {
        $this->$column = $this->$column - $amount;
        
        $this->save();
    }
}
class SomeModel extends Model
{
    use \App\Traits\IncrementDecrement;
}

Im pretty sure it has never fired an event as it doesn’t call save, it calls increment/decrement directly on a builder.

@taylorotwell

What about just saved / updated and not saving / updating? Can we get value from the database after updating? Just saved / updated gonna fit my need for most cases.

Or any other possible way to catch increment/decrement events inside an observer.

Hey … nothing seems to be moving here. Can we have an “official” point of view ? Can we expect such an improvement in the future OR is it not planed at all ?

Thanks a lot

Anyone can help me? I have this view post function errorpost

I also make categories for posts : errorcat

visit count will increase if we view posts. If i view posts at homepage i don’t get error. But if i try to view posts by categories and get this errror :
incr