mardi 28 juin 2016

How do I move Laravel remember token into a separate table?

I'm pretty much a beginner in both Laravel and PHP. I used make:auth to create a simple website that I can log in and out of, and I'm trying to move the remember token of the application out of the users table and into another table called remember_tokens. The latter has user_id as a foreign key. I was hoping to expand on this idea in the future in order to remember multiple devices for the same account, but I was also thinking that it doesn't make sense to store the remember token in the users table since it's just some transient state information separate from what defines the account.

I'm really not sure what needs to be modified and in what respect. I created another model along the following lines:

class RememberToken extends Model
{
    public function user() {
        return $this->belongsToOne('App\User');
    }
}

I then added the following to my User model:

public function rememberToken() {
    return $this->hasOne('App\RememberToken');
}

public function getRememberToken() {
    return $this->rememberToken;
}

public function setRememberToken($value) {
    return $this->rememberToken = $value;
}

public function getRememberTokenName() {
    return 'remember_tokens.remember_token';    
}

I know there are several errors here but I can't find any examples or documentation that might tell me what should really be in these functions in my situation, or what else might need to be added. I came across another StackOverflow question similar to this one, and an answer to that question put me on this track, but it didn't include any code.

Is there a way to do what I want to do? And if so, can it be done without unreasonable performance penalties?

Thanks for your help.




Aucun commentaire:

Enregistrer un commentaire