Files
star-erp/database/factories/ProductFactory.php
2025-12-30 15:03:19 +08:00

71 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Database\Factories;
use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product>
*/
class ProductFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
// 定義豐富的中文測試資料庫
$productNames = [
'高山烏龍茶', '茉莉綠茶', '特級紅茶', '四季春茶', '阿薩姆紅茶', '錫蘭紅茶', '普洱茶',
'珍珠粉圓', '波霸粉圓', '椰果', '仙草凍', '愛玉凍', '布丁', '紅豆', '綠豆',
'果糖', '黑糖漿', '蜂蜜', '蔗糖液', '煉乳',
'奶精粉', '鮮奶油', '全脂鮮乳', '低脂鮮乳', '燕麥奶',
'檸檬原汁', '百香果漿', '芒果果泥', '草莓果醬', '柳橙濃縮汁',
'塑膠杯 (700cc)', '紙杯 (500cc)', '封口膜', '粗吸管', '細吸管', '杯蓋',
'外帶提袋 (單杯)', '外帶提袋 (雙杯)', '吸管套', '杯架'
];
$brands = [
'統一', '光泉', '味全', '雀巢', '立頓', '天仁', '台糖', '義美', '開元', '瑞穗', '六甲田莊', '南亞塑膠'
];
$specs = [
'業務用 5kg裝', '100入/袋', '業務號 3L', '特級品', 'A級', '家庭號 2L', '10kg/箱', '500g/罐'
];
$units = ['包', '罐', '瓶', '箱', '袋', '個', '支', '公斤', '公升'];
$baseUnit = $this->faker->randomElement($units);
$largeUnits = ['箱', '袋', '桶', '磨', '組'];
$largeUnit = $this->faker->randomElement($largeUnits);
$purchaseUnits = ['箱', '棧板', '車'];
$purchaseUnit = $this->faker->randomElement($purchaseUnits);
// 隨機取得一個 Category若沒有則略過
$category = Category::inRandomOrder()->first();
// 為了讓名稱不重複,可以加個隨機形容詞或編號,或者直接用 faker 但指定語系 (若 env 不能改)
// 這裡我們混搭:隨機選一個品名 + 隨機品牌 + 隨機規格,組成較真實的商品描述概念
$name = $this->faker->randomElement($productNames);
$brand = $this->faker->randomElement($brands);
$spec = $this->faker->randomElement($specs);
return [
'code' => strtoupper($this->faker->unique()->bothify('??###')),
'name' => $name . ' (' . $brand . ')', // 例如:高山烏龍茶 (天仁)
'category_id' => $category ? $category->id : null,
'brand' => $brand,
'specification' => $spec,
'base_unit' => $baseUnit,
'large_unit' => $largeUnit,
'conversion_rate' => $this->faker->numberBetween(10, 1000),
'purchase_unit' => $purchaseUnit,
];
}
}