Reading JSON in Seeder0:00
There's two things I want you to remember. The first is we've created a product cedar with the artisan command just before , where we can run some sort of seeding action. And the other one is that we have that data that JSON file collocated in the same directory. And this is what contains this JSON data. So in our products cedar file in the run function, we can use the file facade. There are a lot of them. This is this one here, and it's going to give us a get method.
There are a lot of them. This is this one here, and it's going to give us a get method. So this lets us get any file on the file system. And then we can use another helper data base path, which is the path to the database directory, which remember is this one, which is where our cedar's and product cedar is. And then we can pass the rest of the path here, cedar's slash data that JSON. Okay. So that should work. So this in a data variable and try to dump and die this data.
Running the Seeder0:56
So that should work. So this in a data variable and try to dump and die this data. And let's try run this product cedar. I am blanking out on the exact command, but I can ask copilot, run the product cedar. It should know what to do. There you go. PHP artisan DB seed. And then we set the class to be the product cedar. So let's run that.
And then we set the class to be the product cedar. So let's run that. And look at this, we have all our JSON data. But as you can see, this is a giant string of text. It's definitely a good start though. If I want to turn this into PHP data, I can use JSON underscore D code here. And so I will wrap all of that like that. And let's run the cedar one more time. And aha, if I scroll up this time, you can see that this is an array with nine items.
Creating Products from JSON1:41
And aha, if I scroll up this time, you can see that this is an array with nine items. So now we can work with this data. We managed to get the JSON data in our cedar as a PHP array. This is half the battle. Now, all we need to do is loop over that array and create a product, the product database table that we've created for each instance of the array or for each index of the array, I should say.
the array, I should say. All right. So let's get rid of the dump and die. And instead, let's go for each. And we're going to loop over the data as a product for each item. And here we're going to grab our product model that we've created. And we can call the create method on it. It's at a semicolon. So we don't get squiggles.
It's at a semicolon. So we don't get squiggles. And here, essentially, we're going to map the fields from the data that JSON file into our database table rows. So remember, we have a name field on the database. And this is going to be product name. We can do the same for category. Then let's do the image, but here it's going to be a little bit more complicated and same
Then let's do the image, but here it's going to be a little bit more complicated and same for the price. For now, let's just bring the price along. So this is not going to be a final code. This is not going to work for the price and the image, but let's try run the c edar and see if we can actually bring the nine products in the database. Instead of calling the cedar directly, I am going to go in the database cedar, which
Instead of calling the cedar directly, I am going to go in the database cedar, which is the mothership of all ciders. And here in the run function, call the product cedar. So now I can run the generic PHP, artisan migrate, and let's go fresh to replace the existing data, although there's no data, and then go dash dash seed to run this database cedar, which will in turn call the product cedar. So let's do this.
Fixing Seeder Field Errors3:23
cedar, which will in turn call the product cedar. So let's do this. And you can see it's dropped all the tables. It ran the migrations here, and it should use the product cedar database. But there is an error. What have we done wrong? Oh, oh, I already know what's wrong. The price wasn't priced, but price underscore sense. And you can see here, product has no column name price. Fair enough.
And you can see here, product has no column name price. Fair enough. Let's go fix our product cedar. You might have seen this and yelled at the screen, but I could not hear you price sense. And we're going to run the cedar once again. And this time, hopefully, still an error. My goodness. Oh, of course, I know what's happening. So remember how there's multiple images.
Oh, of course, I know what's happening. So remember how there's multiple images. And I said, I just wanted the mobile image. Well, right now we're trying to map a string to an object with multiple images. So we need to drill down to the mobile image. Third time is the charm. What we want here is the product image mobile. All right, this time, I like my chances. Let's run it again. And whoo, it has worked this time.
Normalizing Price and Image4:24
Let's run it again. And whoo, it has worked this time. So if I go in the database and refresh the database, check it out. We have a nine product in the database. Now there's two problems. I want to change the image path to be just the image name. And the price is not in sense here. So we need to multiply it by a hundred. The price should be very easy, all we have to do is multiply it by a hundred. And now for the image, there's multiple ways probably to do this.
The price should be very easy, all we have to do is multiply it by a hundred. And now for the image, there's multiple ways probably to do this. I will just do a string replace. So str underscore replace. And I want to replace the whole bit I want to remove, which is dot slash assets slash images slash. And I want to replace that with an empty string. So as a result, it should get rid of that segment and make the image look like this.
So as a result, it should get rid of that segment and make the image look like this. And then for the price sense, what I'm hoping is it's going to look like 650 cents. Let's find out. I'm going to wipe the database and run the cedar one more time and see what happens. PHP ad isn't my great fresh dash dash seed. The series ran successfully. And if I refresh the database, we have exactly what I wanted.
Passing Products to View5:28
The series ran successfully. And if I refresh the database, we have exactly what I wanted. The price is in sense. And the image is just the image now that we've got all this data in our database, we can start thinking about doing some UI work, but before we do that, what are we just bring the data to the front end, try to pass it to the welcome that blade that PHP template and then validate that it shows up and then we can move on to styling the UI.
template and then validate that it shows up and then we can move on to styling the UI. Thanks to Laravel. This is going to be incredibly easy in the web.php file, which is our router. We have here a homepage that returns the welcome view. All we need to do is have an array of data that we're going to pass. And here we want the products to be the product model and then column, column, all to pass all the products. Sure.
all the products. Sure. In the case where you had hundreds and hundreds of products, you would not pass all products to the front end. You would have some pagination and maybe also select which fields are passed. But here we have a little data set. It's a challenge. This is perfect. Define.
This is perfect. Define. So now if I go to this welcome that blade that PHP file, we should have access to a products variable that we've passed from the backend. So the quickest way to check that is to go DD and then pass products. And this is the wonderfully looking dump part of the dump and die. You can see we have an array of items and it's our nine products, which will contain fields like the ID name category price sense.
contain fields like the ID name category price sense. If we wanted a slightly less nerdy way to display that information, here's what we could do. We have the dump and die here, let me add some padding so it's not right on the border, but we could have an another list and then use a for each loop with blades like copilot says, but I will just literally have the name that's enough as a proof of
copilot says, but I will just literally have the name that's enough as a proof of concept. And oops, there's a closing tag here that shouldn't be there. And so we should now see the nine product or dessert names on the screen. And this is absolute proof in the pudding that the data is passed to the front end. So now we can switch back to telling CSS and try to build the UI in the next few lessons.
few lessons.
