36587-vm/db/seeders/seed_products.php
Flatlogic Bot f1b278f27d Chep Kart
2025-12-02 15:13:03 +00:00

40 lines
1.1 KiB
PHP

<?php
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../config.php';
function seed_products() {
$faker = Faker\Factory::create();
$pdo = db();
// Fetch category IDs
$stmt = $pdo->query("SELECT id FROM categories");
$category_ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (empty($category_ids)) {
echo "Please seed categories first.\n";
return;
}
$products = [];
for ($i = 0; $i < 300; $i++) {
$products[] = [
'name' => $faker->words(3, true),
'brand' => $faker->company,
'category_id' => $category_ids[array_rand($category_ids)],
'subcategory' => $faker->word,
'image_url' => $faker->imageUrl(640, 480, 'technics', true),
];
}
$stmt = $pdo->prepare("INSERT INTO products (name, brand, category_id, subcategory, image_url) VALUES (:name, :brand, :category_id, :subcategory, :image_url)");
foreach ($products as $product) {
$stmt->execute($product);
}
echo "300 products seeded successfully.\n";
}
seed_products();