fix structure product categories table and crud product
This commit is contained in:
@@ -17,33 +17,40 @@ class ProductAndCategorySeeder extends Seeder
|
||||
{
|
||||
$categories = [
|
||||
'Oli & Pelumas' => [
|
||||
['code' => 'OLI001', 'name' => 'Oli Mesin 10W-40'],
|
||||
['code' => 'OLI002', 'name' => 'Oli Gardan'],
|
||||
['code' => 'OLI001', 'name' => 'Oli Mesin 10W-40', 'subcategory' => 'Oli Mesin'],
|
||||
['code' => 'OLI002', 'name' => 'Oli Gardan', 'subcategory' => 'Oli Gardan'],
|
||||
],
|
||||
'Aki & Kelistrikan' => [
|
||||
['code' => 'AKI001', 'name' => 'Aki Kering 12V'],
|
||||
['code' => 'AKI002', 'name' => 'Regulator Rectifier'],
|
||||
['code' => 'AKI001', 'name' => 'Aki Kering 12V', 'subcategory' => 'Aki'],
|
||||
['code' => 'AKI002', 'name' => 'Regulator Rectifier', 'subcategory' => 'Kelistrikan'],
|
||||
],
|
||||
'Rem' => [
|
||||
['code' => 'REM001', 'name' => 'Kampas Rem Belakang'],
|
||||
['code' => 'REM002', 'name' => 'Cakram Depan'],
|
||||
['code' => 'REM001', 'name' => 'Kampas Rem Belakang', 'subcategory' => 'Kampas Rem'],
|
||||
['code' => 'REM002', 'name' => 'Cakram Depan', 'subcategory' => 'Cakram'],
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
foreach ($categories as $categoryName => $products) {
|
||||
$category = ProductCategory::firstOrCreate(
|
||||
['name' => $categoryName],
|
||||
foreach ($categories as $parentName => $products) {
|
||||
// Create parent category
|
||||
$parent = ProductCategory::firstOrCreate(
|
||||
['name' => $parentName],
|
||||
['created_at' => now(), 'updated_at' => now()]
|
||||
);
|
||||
|
||||
foreach ($products as $product) {
|
||||
// Create child category (sub-category)
|
||||
$child = ProductCategory::firstOrCreate(
|
||||
['name' => $product['subcategory'], 'parent_id' => $parent->id],
|
||||
['created_at' => now(), 'updated_at' => now()]
|
||||
);
|
||||
|
||||
// Create product in the child category
|
||||
Product::updateOrCreate(
|
||||
['code' => $product['code']],
|
||||
[
|
||||
'name' => $product['name'],
|
||||
'description' => $product['name'],
|
||||
'product_category_id' => $category->id
|
||||
'product_category_id' => $child->id
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user