60 lines
2.0 KiB
PHP
60 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\ProductCategory;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class ProductAndCategorySeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run()
|
|
{
|
|
$categories = [
|
|
'Oli & Pelumas' => [
|
|
['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', 'subcategory' => 'Aki'],
|
|
['code' => 'AKI002', 'name' => 'Regulator Rectifier', 'subcategory' => 'Kelistrikan'],
|
|
],
|
|
'Rem' => [
|
|
['code' => 'REM001', 'name' => 'Kampas Rem Belakang', 'subcategory' => 'Kampas Rem'],
|
|
['code' => 'REM002', 'name' => 'Cakram Depan', 'subcategory' => 'Cakram'],
|
|
],
|
|
];
|
|
|
|
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' => $child->id
|
|
]
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|