我试图用两个外键(category_id 和 sub_category_id 分别用于类别和 sub_categories 表)为 Products 表创建播种器。
Category::all()->each(function ($category) {
SubCategory::all()->each(function ($sub_category) {
$faker = Faker::create();
for($i = 0; $i < 3; $i++) {
DB::table('products')->insert([
'product_name' => $faker->name,
'product_description' => $faker->sentence,
'product_price' => rand(100, 1000),
'product_quantity' => rand(10,100),
'category_id' => $category->id,
'sub_category_id' => $sub_category->id,
]);
}
});
});
试过这个,但它返回给我一个错误
未定义变量:类别
我可以只使用 sub_category 创建播种器,但我也需要使用类别创建。我该怎么做?