So we need to store the remembered tokens inside the database.
That's why I'd like to start with changing the schema.
So we need to add a new schema for our remember tokens to make it quicker.
I'm just going to copy and paste some code.
So the table name would be remember underscore tokens.
There are some of the columns that we need here.
For example content can be renamed to token because it will be of type text.
We need the user ID.
So let's keep it.
We don't need post ID and we need another date.
This is expires at.
We need to know when will the token expire.
It doesn't have a default value but it can be null.
We also have a created at timestamp and we can get rid of this post id foreign key as we don't have
that relation.
There it is.
this.
We've got the Remember Tokens table definition.
Okay.
So next up maybe let's create a model.
Let's call it remember token.
We don't really have any strict requirements on how we call the model.
Let's define the namespace that's up models and the classes remember token.
So it needs to extend a base model.
It might be possible the model will also learned some interesting new methods.
But first let's start with the basics.
Let's add a field that defines the table name.
So this is protected static string table.
And this is remember Tokens.
Then let's define all the fields.
This would be public.
Maybe.
Let's try with some typing this time public int which is nullable before we save it.
We don't have an ID so there is an ID.
Then the next one is user ID.
Then we've got the token and expires at.
We should also have a createdat.
And let's jump to schema ID token.
User ID expires at createdat.
Okay.
All five fields are present.
Okay.
Next up let's think how can we generate this token.
So I think that it's a good idea to generate the token within this Who remember talking model so that
when this model is used, no one would have to figure out a way to generate the token by themselves.
So let's add a private static method called generate token.
It's only for internal use.
We're going to generate the token a simple way.
First we'll generate some random characters.
The length is 32 since this is random bytes, so it might not generate Ascii characters, but also bytes
with some random meaning.
We convert that from binary to hexadecimal, so it looks like a normal string.
So every token needs to expire at certain time, and this expiry date will be stored not only inside
the database, but also inside the cookie.
We might begin by adding a constant.
Here this would be private constant and let's call it token lifetime.
It needs to be reflected in seconds.
So I multiply 30 days.
That would be my default times 24 hours.
Times 20 minutes times 20.
Sorry times 60 minutes and 60s.
Next up let's add a method called get expiry date and it will return a string because this one it will
generate a date in a specific format, which is year, month, day, hour, minute, second.
We generate it by getting the current time, which was returned in seconds.
And to that we are adding the constant which is the token lifetime.
Okay.
So next let me add a public static method.
We're going to call it create for user.
And we pass the user id that's an integer.
And it returns static which essentially means please return an instance of an remember token class.
So what we will do here is we will wrap the create method that exists in a model as you see.
It accepts the data.
We've used this this method previously and we just pass some values for the columns.
So for user ID we are passing the value of the passed argument.
But some of those columns will have the values generated like the token.
We've just created this generate token method and also the expiry date.
We also have a method that can give us an expiration date.
There we have it.
So every single time you want a new remember token you can obviously create it using the create method.
That's possible.
But this seems to be a more handy way because you don't need to worry about the token or the expiration
date, it just generates the right token, the right date.
You only need to pass a user and the token is saved.
Okay, so we have created the model, some basic methods and the schema.
Next up we've got more work to do with the remember token.
We need a method to find a valid token and another one to rotate the token which means to regenerate
it.
For that, I'd like us to also add something to the base model class.
A method that will allow us to call save method on an instance of the model object.
And this would either create or update database record with all the defined fields.
That's also something that exists in Laravel.
And we're going to see a simple way we can implement that.
And since it might take a little longer I want us to take a short break right now.