Stop Update the update_by field of user table while Login in Laravel application

I have the following code inside my User model.

public static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            if ($user = Auth::user()) {
                $model->created_by = $user->id;
                $model->modified_by = $user->id;
            }
        });
        static::updating(function ($model) {
            if ($user = Auth::user()) {
                $model->modified_by = $user->id;
            }
        });
    }

On login, as the remember_token is updated in the user table, the updated_by is also updated.
is there any way to stop updating on special occasions like login ??

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In the updating event you can check, if the current value of remember_token is same as old one (with getOriginal method). And only then update also modified_by.

static::updating(function ($model) {
    if ($user = Auth::user()) {
        if ($model->remember_token == $model->getOriginal('remember_token')) {
            $model->modified_by = $user->id;
        }
    }
});


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x