So let's keep going right now, we have our handlers doing nothing more than grabbing information from
our transaction and from the form and displaying a receipt to the end user.
And of course, we want to save the transaction and the other information.
Now, if you think this through, if we go back to our form.
So here's our buy widget for right now.
I'm charging a credit card, which is fine.
I don't want to store that information, but I do get the last four digits and I do get the expiry date.
And we have nowhere to put our cardholder name and our cardholder email.
And of course, in production, you might be getting address, information, special instructions,
whatever it may be.
But for our purposes, I'm going to consider a customer nothing more than a name and an email address
and any orders associated with that customer and any transactions associated with that customer.
So we need somewhere to put this information.
So let's go back to our I.D. and let's open our terminal and let's create a new database table.
And let's also at the same time add the expiry month and expiry year to the transaction table.
And that's really easy using soda.
So I'm in the root level of my application.
I type so to generate and then Fiz is the format I want to generate and I'll just call this create a
customer table, so create customer table and that will generate two new migrations up and down.
So let's find our migration's folder and let's open the up migration.
Now, this is going to be creating a table I want I want to do to save some time as our users table
is almost identical.
In fact, we could use the user's table, but I'm not going to I'm going to create things separately
so, so openly.
Create users, table up is select all and copy that.
And then I'll open the new transaction or a new migration, which is create customer table up pastry
and just change this to customers.
And I'll get rid of the password field because we're going to make this simple.
We're not going to have people log in.
Everyone is a new customer and I'll put this to be customers.
And this is just to set the default values for creating an updated to customers.
And I'm not going to insert anything into it, so I'll delete that.
Perfect.
And I will do the down migration, which is good practice.
So we'll say drop underscore table and then it's just customers.
OK, so that one's done.
So let's clear the screen in our terminal.
And so to generate fees and calls to transactions
and we'll open the migration for that.
And actually we have one here that adds a column.
So again, I can copy and paste just to make it simpler.
So I'll copy that.
That's add image to widgets, update fees, and now I'll open the up transaction or migration that we
just created.
And our table is transactions and we want expiry month.
Now what's the type for those?
Go to handlers and find out.
So expiry month right here that comes as a you in sixty four.
I'll just convert it to an end, so I'll leave it back to my migration and call that an integer and
we'll give it a default value of zero and I'll duplicate this row and change that one to expire.
OK, let's do our down migrations and again I'll copy the ad image to widgets down one and just modify
that copy and open the ad calls to transactions down Paiste and I'm going to drop from transactions.
Then I'm going to drop expiry month and I'll duplicate this and drop expiry year.
OK, so now I should be able to run so to my group and there.
So now we have we now have a customers table and we've added those two columns to our transaction's
table.
So that means we need to go over to our models.
So let's find models.
Right here, and I'll find the user model and copy
and paste this in here, so user will become customer.
Customer and customers, and there is no password field, so I'll delete that now.
Are we done?
Let's think this through.
I have a customer, I have a transaction and I have an order.
What kinds of things?
Things am I going to need to associate customers with orders?
Well, if we go to order, we have a transaction ID.
Why don't we just add a column to that as well called.
Customer and in Jason, we'll call that customer underscore ID.
OK, so now we need to add that column, customer IDs to orders so we can get from our customer to an
order by customer ID and then from a transaction from from an order to a transaction by Transaction
I.
And that seems to be a pretty simple relationship.
So that means, of course, we'll have to go and clear our street in the terminal and type.
So to generate fees and customer ID two orders
and that gives me two new migration's.
So we'll open those up.
So we have this one and we have this one.
And I'll just again copy and paste so I'll see you select copy.
Close that.
Finally up transaction remigration for add customers and columns to customers.
Delete that.
And this would be to go to add customer to orders up.
That's the one I'm working with and customer ID two orders up.
So we want to put this into orders.
We want this to be customer ID, we want it to be an integer and it needs to be exactly the same type
as the ID in customers, which is an integer.
So we'll make this unsigned through.
OK, so that should be the correct format for that.
Now, I also want to create a foreign key relationship between the field customer ID in orders to the
field I.D. in customers.
So the syntax for that in Fiz is and underscore foreign.
Make sure you spell that right.
Underscore key.
We want to put this on orders and we want it to be on the field customer ID.
And the relationship is and this is in curly brackets to the table customers, then a colon, then square
brackets.
And that has to be in quotes then between the closing curly bracket and the parentheses, put a comma
and more curly brackets.
And you have two things to handle on delete.
And I'll just say Kaskade.
And on update also Kaskade, OK, that's a little tricky to write, and if you have trouble getting
it, you can always download the source code for this lecture and copy my migration's.
If you're finding it problematic, and I'll be honest with you, I usually make a mistake when I type
of the foreign key.
For some reason I'm not sure why.
OK, so there's our column and now we can go to our down, so I'll copy the down migration from ad calls
to transactions.
I copy one of these
and this down migration will become a drop column orders customer writing.
OK, now if we got everything right, I should be able to run that migration.
So to my right and there it is.
OK, so we've got these set up now that those are set up.
Of course, what's going to happen next is if we go to our handlers or we're going to do here and it
might not be the payment succeeded handler that we use.
Ultimately, we might make a different receipt page and checkout process for customers on the front
end.
But what we're going to do, in essence, is, again, pass the form, grab the information from the
form because we need all this information.
Then we're going to generate a card and we're going to get a payment intent and get the payment method.
None of that will change.
But once we have this information at this point, we're going to save the customer information to the
customer table in the database, which we haven't written yet.
We'll have to write that little bit of school and the associated methods, and that's straightforward.
Then we'll get the customer ID from that insert, then we'll create an order ID and we'll insert an
order, including the customer ID and all the information necessary to populate the orders table.
And when we get that back, then we'll create a transaction and insert the transaction information,
or we might do it in a slightly different order.
But that's the general gist of what we're going to do next.
OK, that's enough for this time around.
We have a place to store this information.
We've updated the tables as necessary and we've updated the models as necessary.
So in the next lecture, we'll get started saving some actual information to the database.