Last Login timestamp behavior

I’m surprised i cant seem to find the right solution for this but i’m not sure i understand docs correctly.

I want to implement a simple last_login feature, to save when a user logs in.

I don’t want to just save the attribute in the login function, i would like to use the ‘correct’ event for it.

But i see blameable and timestamp behavior, which i can’t figure out which one it should be.

I’m already using timestamp behavior but it works something like




ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],



which i don’t think is what i want.

But then i cant figure out the correct way to use blameable either.

Any pointers please, very surprised i cant find it anywhere unless im not searching the right terms

Try this in your User Model




public function init() {

        parent::init();        

        Yii::$app->user->on(\yii\web\User::EVENT_AFTER_LOGIN, [$this, 'addLastLogin']);        

}

    

public function addLastLogin()

{

   	$this->touch('last_login'); // last login db field

}

beautiful, thankyou