在登录 Laravel 应用程序时停止更新用户 table 的 update_by 字段

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

我的用户模型中有以下代码。

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;
            }
        });
    }

登录时,随着 remember_token 在用户 table 中更新,updated_by 也会更新。 有没有办法在登录等特殊情况下停止更新??

在更新事件中,您可以检查 remember_token 的当前值是否与旧值相同(使用 getOriginal 方法)。然后才更新 modified_by.

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