您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CurrencyFactory.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Database\Factories;
  3. use App\Models\Setting\Currency;
  4. use Illuminate\Database\Eloquent\Factories\Factory;
  5. /**
  6. * @extends Factory<Currency>
  7. */
  8. class CurrencyFactory extends Factory
  9. {
  10. /**
  11. * The name of the factory's corresponding model.
  12. *
  13. * @var string
  14. */
  15. protected $model = Currency::class;
  16. /**
  17. * Define the model's default state.
  18. *
  19. * @return array<string, mixed>
  20. */
  21. public function definition(): array
  22. {
  23. $currencies = config('money');
  24. $existingCodes = Currency::query()->pluck('code')->toArray();
  25. foreach ($existingCodes as $code) {
  26. unset($currencies[$code]);
  27. }
  28. $randomCode = $this->faker->randomElement(array_keys($currencies));
  29. $code = $randomCode;
  30. $currency = $currencies[$randomCode];
  31. return [
  32. 'name' => $currency['name'],
  33. 'code' => $code,
  34. 'rate' => $this->faker->randomFloat($currency['precision'], 1, 10),
  35. 'precision' => $currency['precision'],
  36. 'symbol' => $currency['symbol'],
  37. 'symbol_first' => $currency['symbol_first'],
  38. 'decimal_mark' => $currency['decimal_mark'],
  39. 'thousands_separator' => $currency['thousands_separator'],
  40. 'enabled' => $this->faker->boolean,
  41. 'company_id' => $this->company->id,
  42. ];
  43. }
  44. /**
  45. * Indicate that the currency is enabled.
  46. */
  47. public function enabled(): Factory
  48. {
  49. return $this->state(static function (array $attributes) {
  50. return [
  51. 'enabled' => true,
  52. ];
  53. });
  54. }
  55. /**
  56. * Indicate that the currency is disabled.
  57. */
  58. public function disabled(): Factory
  59. {
  60. return $this->state(static function (array $attributes) {
  61. return [
  62. 'enabled' => false,
  63. ];
  64. });
  65. }
  66. }