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

Creating Product Seeders0:00

Okay, let's move on and create some seeders. So we have some demo data to work with, and we can start building up the front end of the web show. If you open the database folder, you can see that there are a couple of subfolders. We have Factories, Migrations, and Seeders. By default, there's a single database seeder, and there are some examples here that you can see that it sets up a User, and in this case, we want to see our database and make sure that we have our product, that we have some variants, that we have images, and we don't need a seeder for the cards or the card items at this time, but anyways, let's start and seed our products.

don't need a seeder for the cards or the card items at this time, but anyways, let's start and seed our products. Let's remove this boilerplate that we have right here, and we're going to say Product, Factory, and we can say create. Now it is possible to provide our attributes manually, but it's also possible to set up the data that we want to generate inside our factory itself. So if we switch to our ProductFactory, you can see that we have a definition here where we can provide our seeder with some fake data. So if we look again at our create products table, we have a name, a description, a price, an image_id.

Defining Product Factory Data1:19

So if we look again at our CreateProducts table, we have a name, a description, a price, an image_id. So here we can say this faker, and we can provide it with the value that we want to fake. So don't think there's a product name or something, but what we can do is provide a random element. So now we can provide it with an array, and what happens, it will simply choose a random value from this array. So let's provide some product names. So we have a purple cap, we have a t-shirt, a blanket, what else, maybe a sweater. So now whenever we generate a product with our factory, it will select one of these random.

So we have a purple cap, we have a t-shirt, a blanket, what else, maybe a sweater. So now whenever we generate a product with our factory, it will select one of these random elements. And of course, we need to make sure that we provide the key that we want to use this for, so in this case, name. Next we have description, and I think there's a paragraph. So we can say we want to have a description of, say, two paragraphs. Okay, next we got price. Let's provide a random number to choose from, and as you might remember, we're using cents. So in this case, 100 will be equal to $1, so let's maybe set a minimum of five and a

Updating Image Schema2:25

Let's provide a random number to choose from, and as you might remember, we're using cents. So in this case, 100 will be equal to $1, so let's maybe set a minimum of five and a maximum of, I don't know, 45. So now, in this case, it might occur that LaravelCap is being included twice because it's not checked for uniqueness. So what we can do here is call a unique method, and this will ensure that only one of these random elements will be chosen whenever we see the database. Now we also have the mainImageId, and I've been thinking maybe we should put the mainImage or featuredImageId on the Image model itself. So what I'm going to do is I'm going to remove this column, I'm going to switch back to our

image or featured image ID on the Image model itself. So what I'm going to do is I'm going to remove this column, I'm going to switch back to our create_images_table, and I'm simply going to add a boolean flag here and call this featured. And by default, it's set to false. So let's move back to our database_seeder and select that we want to create four different products in this case. So let's give it a try, php artisan migrate, php artisan db:seed, and then it is done seeding our database. Now if we would run php artisan tinker, and let's say products::all(), it doesn't recognize that, let's make sure that we use the entire namespace.

Seeding Product Variants3:37

Now if we would run php artisan tinker, and let's say products all, it doesn't recognize that, let's make sure that we use the entire namespace. And as you can see, now we have our products with name blanket, we have a description, we have a random price, our sweater, our t-shirt, perfect. So our product database is now seeded. Next we can move on and continue seeding our product variants. And let's set a definition. So if we take a look at our create_product_variants table, you can see that we have our product_id, we have a color and a size. So let's switch back and let's define color, and let's use the random element again and

product ID, we have a color and a size. So let's switch back and let's define color, and let's use the random element again and set a couple of colors. Now we'll do the same for the size. Okay, there we go. Now productVariants has a relationship with our product. So in order to make sure that our seeder understands this, we need to define this relationship first. So let's head over to our Product model, and let's set up this relationship. So the public function variants, and we're going to make sure that this returns hasMany.

So let's head over to our Product model, and let's set up this relationship. So the public function variants, and we're going to make sure that this returns hasMany. We'll pass the ProductVariant model, and that should work. Now we have a relationship set up. So now when we head back to our database seeder, we can simply instruct it to include variants as well when it is creating our products. And we can simply do this by calling the has followed by the relationship. So in this case, hasVariants. So now we can define how many variants that we want to have. So let's say that each product has, I don't know, five variants.

So now we can define how many variants that we want to have. So let's say that each Product has, I don't know, five variants. Now we should refresh our entire database and seeder again, and run php artisan tinker again, and get our models, and make sure that we include all our variants. You can see that we have our products, and there we go. There we have our variants. So it is correctly matched up with our product ID. We have our color orange and size S, and we have a bunch of other variants. Perfect. So having seeders is really convenient because it allows a different developer to instantly

Adding Product Images5:46

Perfect. So having seeders is really convenient because it allows a different developer to instantly boot up the project and have their database seeded and try out the application without having to figure out what data needs to be put inside the database in order to have a working application. And this allows us as well to simply start scabbling out the front end without having to deal with manually creating products every time. Okay, next, we need to make sure that our product has a few images as well. What we can do here is we can say hasImages. Let's say that each of our products has three images.

What we can do here is we can say hasMany images. Let's say that each of our products has three images. This relationship doesn't exist yet, so let's make sure that it does. So let's move over to our Product again, and let's find another method in this case called images. This is a hasMany relationship as well. We'll pass our Image class, and that's all set. Now let's move over to our ImageFactory, and if we look at our create_images table, you can see that we have our product_id, which is our relationship. We have our featured column, whether an image is featured or is the primary image of a product,

you can see that we have our productID, which is our relationship. We have our featured column, whether an image is featured or is the primary image of a product, and we have a path to our image. So let's switch back, and let's in this case set our path, and let's use the faker and new randomElement again, and provide it with just a few random images that we'll put inside our public directory of our application. So let's set the path to media/example1.png, and let's repeat this for, I don't know, five different images, and let's make sure that these are unique as well, so we don't get a product with duplicate images. So when we try to migrate a database at this time, we get an error.

don't get a product with duplicate images. So when we try to migrate a database at this time, we get an error. So if we switch back to our database here, you can see that we have three images defined, but it's running into an issue that there aren't enough images to be used for the set amount of products that we have. So we need to make sure that there are a couple of more options. So now if we try again, you can see that the database here was successful, because there were enough unique images for each product. So let's tinker again, and this time let's make sure that we include our images as well, to verify that everything is working.

So let's tinker again, and this time let's make sure that we include our images as well, to verify that everything is working. So as you can see, we have our model, we have our ProductVariants, and here we have our images. So at this time, none of our products have a featured image, and of course we want each product to have a featured image, so this is what we can show whenever we are listing our products, and of course we don't want to show all the images. So if we head over back to our database here, you can see that we don't have any control on how to do this right now. So let's make sure that we change this.

Setting Featured Image8:36

on how to do this right now. So let's make sure that we change this. So instead of using this magic method, we're going to say has, and we're going to provide the factory instead. So we're going to say image, factory, we're going to again say that we want to have three images. Next, we're going to call sequence, and sequence is basically a method that you can call, which will be ran every time a new image is initiated. So this method will be called three times, and it accepts a closure that provides a sequence check.

So this method will be called three times, and it accepts a closure that provides a sequence check. And what we basically want to do is whenever we're at the start of the sequence, we want to make it featured, or in other words, the first image that is created is going to be our featured image. So we can return an array of attributes that we want to modify, so in this case, we want to modify the featured column on our model, we're going to say sequence, you can see that it provides an index or a count, in this case, whenever our index is set to zero, we want to make sure that it is featured. And if you refresh our database, and run a quick php artisan tinker, and we scroll down to our first

we want to make sure that it is featured. And if you refresh our database, and run a quick php artisan tinker, and we scroll down to our first image, you can see that our first image is featured, and our second image is not. So it's a pretty cool way to hook into the factory. So of course, you can use the measure methods, but it doesn't give that much control. And whenever you need more granular control, you can simply call the factory manually like we've done just now. And it gives you all the factory features that are available, so you can make any adjustments that you need.

that you need.

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