53 lines
1.5 KiB
PHP
53 lines
1.5 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'],
|
|
['code' => 'OLI002', 'name' => 'Oli Gardan'],
|
|
],
|
|
'Aki & Kelistrikan' => [
|
|
['code' => 'AKI001', 'name' => 'Aki Kering 12V'],
|
|
['code' => 'AKI002', 'name' => 'Regulator Rectifier'],
|
|
],
|
|
'Rem' => [
|
|
['code' => 'REM001', 'name' => 'Kampas Rem Belakang'],
|
|
['code' => 'REM002', 'name' => 'Cakram Depan'],
|
|
],
|
|
];
|
|
|
|
|
|
foreach ($categories as $categoryName => $products) {
|
|
$category = ProductCategory::firstOrCreate(
|
|
['name' => $categoryName],
|
|
['created_at' => now(), 'updated_at' => now()]
|
|
);
|
|
|
|
foreach ($products as $product) {
|
|
Product::updateOrCreate(
|
|
['code' => $product['code']],
|
|
[
|
|
'name' => $product['name'],
|
|
'description' => $product['name'],
|
|
'product_category_id' => $category->id
|
|
]
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|