Hello,
I'm currently got stuck with the following: I'm trying to implement a catgeory system to my app. Therefore, I need about 100-200 categories with some "parent categories". So I want to create two tables: Category, Subcategory. The category table holds the parent categories, the subcategories are the subcategory of those, pointing to the primary key of the parent category.
Here's what I mean with the category thing:
Parent Category: Clothing Subcategory: Woman Shoes Subcategory: Men's Shirts Subcategory: Jeans
Parent Category: Handys Subcategory: Smartphones Subcategory: PDA's Subcategory: Smartwatches
and so on.
The database would then have the two tables, categories would hold (for this example) two entries
id name
0 Clothing
1 Handy
and subcategory would look like:
id name parent
0 Woman Shoes 0
1 Men's Shirts 0
2 Jeans 0
3 Smartphones 1
4 PDA's 1
5 Smartwatches 1
I hope you got what I mean. Now, of course I create a migration for those two tables, and of course, I create a model for those two tables. But whats the best way to seed this data into the tables. As I said it are like 100-200 categories (not just those two mentioned). So something like that:
DB::table('category')->insert([ 'name' => 'Clothing', ]);
times 100 wouldn't make sence I guess. Especially, because the subcategory refers to the id of the category. So if I once insert a category between two others, then every category afterwards has another primary key (after new seeding), so everything would be messed up.
So how can I do that? Any ideas?