در حال بارگذاری ...

Store Features Overview0:00

Let's take a look at how we are going to scaffold out the models for our store. I like to take a real-life example and build and scaffold out the models based on this example. So what I've got here is the Laravel merchandise store. Here you can buy your t-shirts, a blanket, a mug, a cap. Pretty cool, check it out. And basically, we want to replicate the functionality that we have right here. So you can see we have an overview of our products. We can click a product and we get a nice, more in-depth page with some extra descriptions about our product. And we have a bigger image of our product.

with some extra descriptions about our product. And we have a bigger image of our product. And you can see that I can select a variant. In this case, we got a size variant. There are some other variants that might include a color, for example. This all depends on the type of store that you're building. But we're going to keep it simple and focus on adding support for a size variant and a color variant. And there's, of course, the cart. So there are no products here right now.

And there's, of course, the cart. So there are no products here right now. So we can check it out if we add this cool t-shirt to our cart. Then we get an overview of all the products in our cart that include the variant, the price, the quantity, and the option to continue to checkout or shopping. And if we go to the checkout page, we want to replicate this functionality as well so that a User can enter his first and last name, email, phoneNumber, shippingDetails, that they get a summary of the order.

Scaffolding Product Model1:50

so that a user can enter his first and last name, email, phone number, shipping details, that they get a summary of the order. And yeah, finally, we'll make sure that they can head over to Stripe to complete their payment. And once their payment is complete, that they get a nice email notification. So I think this is going to be the base for our store. So let's move over to PhpStorm and scale out our models. So the first one is, of course, a Product. So we're going to use php artisan make:model Product.

Creating Artisan Aliases2:25

So we're going to use php artisan make:model. I'm going to say Product. And I like to generate a migration and a factory as well. That's all set. Now, I'm writing php artisan make:model. I prefer to create custom bash aliases. So it's easier and it requires me less typing to run the artisan command. So these are some of my personal preferences. You can either copy these or use your own or simply type out the entire php artisan command.

You can either copy these or use your own or simply type out the entire php artisan command. So my preference is to have artisan as a shortcut to php artisan. I like to have a fresh command which will drop the current database and migrate everything from scratch and generate the seeders. And I'll show you how to do this. So I've got this shell kind of thing installed called OhMyZsh. That makes it a bit easier and gives you a nice output. But if you do a quick Google search on how you can add aliases to your bash profile, you will probably find an easy result on the first listing.

But if you do a quick Google search on how you can add aliases to your bash profile, you will probably find an easy result on the first listing. In this case, I'm using OhMyZsh. So let's take a look at how this works. I'm basically going to say phpStorm. And I already made this file. So this lives in OhMyZsh. And then there's a custom directory where you can basically create new files that get loaded automatically. So I created a custom file called Laravel.

that get loaded automatically. So I created a custom file called Laravel. And if I open it up, I can add aliases here. So I'm going to create an alias and name it art. And that's going to run php artisan. And the other one, alias fresh, is going to run php artisan migrate:fresh and seed the database. All set. You can see if I run art, I get the Laravel Artisan output.

Adding Variant and Cart Models4:39

All set. You can see if I run artisan, I get the Laravel Artisan output. And if I run php artisan migrate:fresh, you can see that it's migrating the database again and seeding the database from scratch. So we already created our products table. Let's see what else we got. So if we go back to our products, then we can see that besides products, we're going to have a variant. So let's create a model for this as well. So now I can use php artisan make:model ProductVariant.

So let's create a model for this as well. So now I can use php artisan make:model Product, php artisan make:migration, and php artisan make:factory again. Okay. So we can add products to our cart. So I think it's good to have a Cart model as well. And I think the Cart can be associated to a User that's currently logged in, but also to a session-based user that hasn't created an account yet. So let's make a model for our Cart. And our Cart, of course, can contain products.

Defining Migration Columns5:39

So let's make a model for our Cart. And our Cart, of course, can contain Products. Let's make a new model. And this time, we're going to call it CartItem. Let's head over to our database migration and add the appropriate columns. So first, let's open our products table. And if we look at our products, we know that it obviously has a name. And of course, we have a price. It's considered best practice to store money or currency as an integer and store it as cents instead of using a float.

It's considered best practice to store money or currency as an integer and store it as cents instead of using a float. So I'm going to use this as well in our example. Instead of storing $19.99, we're actually going to store $19.99 like this. Let's see. We have an image. We're going to make this an integer as well. We're going to say mainImage. Going to make this nullable in case a product doesn't have an image. Actually, let's call this mainImageID.

Going to make this nullable in case a Product doesn't have an image. Actually, let's call this mainImageId. And we have a description. Let's make sure that we have a model for our image as well. So we're going to create a model called Image and create a migration and a factory as well. Next, let's head over to our ProductVariant. Let's set an integer for our productId. So in our example store, we only have one variant, but I think it's cool to add an additional variant as well.

So in our example store, we only have one variant, but I think it's cool to add an additional variant as well. So we got the size, but also let's add a color. So we head over back to our product_variants migration. So let's add a column for our color. Of course, it's possible that a product doesn't have a color variant, so we'll make it nullable. And we'll do the same for size. If your product_variants have more options, you can, of course, append these to this specific table.

If your product variants have more options, you can, of course, append these to this specific table. Okay, let's move over to our cart. So a Cart belongs to a User. In this case, we want to make it nullable because in some cases our User has not logged in yet or he or she doesn't have an account. So we're going to add a string and add a session_id column as well. So if a User is not signing in or doesn't have an account, he or she is going to be assigned a session_id.

So if a User is not signing in or doesn't have an account, he or she is going to be assigned a session ID that we can use to reference the cart. Once a User logs in or registers an account, we can switch over from a session ID to a user ID. Okay, let's move on to our cart items. In this case, we're going to have an unsigned integer and we're going to reference our cart. We'll have a reference to our product variant. And finally, we have a quantity.

We'll have a reference to our ProductVariant. And finally, we have a quantity. And finally, we have our image table. So a Product can have multiple images. So we're going to say table and we're going to create an integer as well that's going to reference our product_id. And we have a path or URL to our image. I think that will cover the basics for our models. In the next video, we're going to look at seeding our database.

Next: Database Seeding9:49

I think that will cover the basics for our models. In the next video, we're going to look at seeding our database and using model factories so we have some data to work with.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟