parfum/database/factories/VoucherFactory.php
Yoga Pangestu c4b90acf53 feat: membuat factory seluruh model
- menyesuaikan model jika ada yang salah seperti kurangnya trait SoftDeletes, casting yang salah, relasi yang kurang dan lainnya
- memperbaiki enum di migrasi
2025-12-09 08:25:07 +07:00

48 lines
1.7 KiB
PHP

<?php
namespace Database\Factories;
use App\Enums\IsShow;
use App\Enums\VoucherType;
use App\Models\Voucher;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class VoucherFactory extends Factory
{
protected $model = Voucher::class;
public function definition(): array
{
$type = $this->faker->randomElement(VoucherType::values());
return [
'name' => $this->faker->words(2, true),
'code' => Str::upper(Str::random(8)),
'tags' => $this->faker->words(1, true),
'type' => $type,
'discount_amount' => $type === VoucherType::PERCENTAGE->value
? $this->faker->numberBetween(5, 50)
: $this->faker->numberBetween(10000, 100000),
'min_purchase' => $this->faker->optional()->numberBetween(50000, 200000),
'max_discount' => $type === VoucherType::PERCENTAGE->value ? $this->faker->numberBetween(10000, 50000) : null,
'quota' => $this->faker->optional()->numberBetween(50, 500),
'limit_per_user' => $this->faker->optional()->numberBetween(1, 5),
'summary' => $this->faker->sentence(),
'start_date' => $this->faker->dateTimeBetween('tomorrow', '+1 week')->format('Y-m-d'),
'end_date' => $this->faker->optional()->dateTimeBetween('+1 day', '+2 months')?->format('Y-m-d'),
'is_show' => $this->faker->randomElement(IsShow::values()),
];
}
public function show(): Factory
{
return $this->state(fn () => ['is_show' => IsShow::SHOW]);
}
public function hidden(): Factory
{
return $this->state(fn () => ['is_show' => IsShow::HIDDEN]);
}
}