Backend Data Modeling0:00
All right, let's pivot to the backend for this one. We're going to start thinking about our database, our data models, and try to bring the JSON data that is part of the challenge into our application as seed data. Let's go do this. Looking at our design, we have a series of desserts, which I guess could be meals or even products if we thought a bit more global. And you can see that they clearly have an image,
if we thought a bit more global. And you can see that they clearly have an image, a name, maybe a category and a price. And we have a shopping cart. So all of these, in terms of backend concerns, need to leave in some sort of shape. Conveniently, the challenge gave us a data that JSON file, which contains the data for all of our products. So you can see that each product has an array of images or an object of images for thumbnail, mobile, tablet,
Simplifying Product Images0:47
So you can see that each product has an array of images or an object of images for thumbnail, mobile, tablet, desktop, and then name, category, and price. To be completely honest with you, this looks like a lot of images. Yes, we could have an image source set and then have a different image for each viewport width. But I think what I'm going to do here is just have one image. It's not going to be as performance, but it's going to save us a ton of work.
It's not going to be as performance, but it's going to save us a ton of work. And then if we need different cropping mechanism, we can use object feed cover. And so the image will always respect an aspect ratio. So bear with me, we're going to use just one image, and this is going to be the mobile image. All right, so what we want to do here is create a new database table for these desserts or meals or products,
Generating Model and Migration1:30
is create a new database table for these desserts or meals or products, and make sure that each table row has the proper fields to match the image, the name, the category, and the price. We want to generate a few things, a database migration, a model for the products, and also a cedar, so we can seed the data from this data.json file into the database. By the way, isn't that funny that I say data and data interchangeably,
By the way, isn't that funny that I say data and data interchangeably, and I don't even know which one is right? All right, so let's use the PHP artisan command to make a, let's start with the model. I like to do it this way, because it is going to ask me questions of what else I want. So we are going to call this product. Like I said, it could be called dessert or meal, but product is a bit more catch-all.
Like I said, it could be called dessert or meal, but product is a bit more catch-all. And then it's going to ask me what else I want, and I want a database cedar, database cedar maybe, and I want a migration. And we don't really need a factory because we're going to seed the data from the JSON data. So let's go with that, and this will have created three files, the model, the migration, and if I scroll up the,
Defining Product Table Fields2:34
and this will have created three files, the model, the migration, and if I scroll up the, oops, this was the cedar, the migration, and the model is up here. And here, the first thing I'm going to work on is the migration file. So Laravel scaffolded a new table with a primary key of ID and timestamps, but here we want to bring along, basically the fields we've seen in the data that JSON file.
but here we want to bring along, basically the fields we've seen in the data that JSON file. Remember, I only want one image, so we have image, name, category, and price. And you know what, thinking in terms of database design, the category could be its own model, and then we could have a relationship, but here we will just use that as a string. This can always be changed later. So I will start with the most obvious one,
This can always be changed later. So I will start with the most obvious one, which is the name and the category, assuming it's a string as well. So table, string, name, and no, not that at all. Table, string, category. These are the easy ones. Next for the image, I will actually also have a string, but what I think we'll do is get rid of that segment, and then basically just have the image file name.
Storing Prices as Cents3:36
but what I think we'll do is get rid of that segment, and then basically just have the image file name. And then we can construct the path to the image in our template. So table, string, image. And the last one we need to do is the price. So here we need to have some consideration. So you can see the price is 8.0, and then 5.5. So this is sort of a semi-formatted price here, but a common practice in e-commerce
So this is sort of a semi-formatted price here, but a common practice in e-commerce is to have the price in sense, because you can calculate whatever formatting you want, but it's a little bit more safe in terms of making calculations. So what I'm thinking here is we store the price as price_sense to make sure that it's clear that it's sense, and then we can multiply by 100 this value when we import the data in the cedar.
Running Migrations and Seeding4:20
and then we can multiply by 100 this value when we import the data in the cedar. Okay, so table, and we are not going to use a decimal, but we are going to use a unsigned integer, which basically is a positive only number. We never want a negative price, I guess. This would be the job of a coupon or something. But here we want to call this price_sense. And with that, I think this is mapping data pretty well. And so I'm going to go and run the database migration
And with that, I think this is mapping data pretty well. And so I'm going to go and run the database migration to create this table in the database. PHP, autism, migrate. And there we go. You can see it's created a new products table to the database. So if we were to open our SQLite database/database/SQLite in a tool like TablePlus, you can see we have this products database
in a tool like TablePlus, you can see we have this products database with the fields that we've created, and it's currently empty. So there's only nine or 12 products in our data.json file. So we could manually really quickly insert them, but it's much cooler to try to import that data through a database cedar and then run the cedar to populate the database table we've just created. Let's try that.
Let's try that.
