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

ContactFactory.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Database\Factories\Common;
  3. use App\Models\Common\Contact;
  4. use Illuminate\Database\Eloquent\Factories\Factory;
  5. /**
  6. * @extends Factory<Contact>
  7. */
  8. class ContactFactory extends Factory
  9. {
  10. /**
  11. * The name of the factory's corresponding model.
  12. */
  13. protected $model = Contact::class;
  14. /**
  15. * Define the model's default state.
  16. *
  17. * @return array<string, mixed>
  18. */
  19. public function definition(): array
  20. {
  21. return [
  22. 'company_id' => 1,
  23. 'first_name' => $this->faker->firstName,
  24. 'last_name' => $this->faker->lastName,
  25. 'email' => $this->faker->unique()->safeEmail,
  26. 'phones' => $this->generatePhones(),
  27. 'is_primary' => $this->faker->boolean(50),
  28. 'created_by' => 1,
  29. 'updated_by' => 1,
  30. ];
  31. }
  32. protected function generatePhones(): array
  33. {
  34. $phones = [];
  35. if ($this->faker->boolean(80)) {
  36. $phones[] = [
  37. 'data' => ['number' => $this->faker->phoneNumber],
  38. 'type' => 'primary',
  39. ];
  40. }
  41. if ($this->faker->boolean(50)) {
  42. $phones[] = [
  43. 'data' => ['number' => $this->faker->phoneNumber],
  44. 'type' => 'mobile',
  45. ];
  46. }
  47. if ($this->faker->boolean(30)) {
  48. $phones[] = [
  49. 'data' => ['number' => $this->faker->phoneNumber],
  50. 'type' => 'toll_free',
  51. ];
  52. }
  53. if ($this->faker->boolean(10)) {
  54. $phones[] = [
  55. 'data' => ['number' => $this->faker->phoneNumber],
  56. 'type' => 'fax',
  57. ];
  58. }
  59. return $phones;
  60. }
  61. public function primary(): self
  62. {
  63. return $this->state([
  64. 'is_primary' => true,
  65. ]);
  66. }
  67. public function secondary(): self
  68. {
  69. return $this->state([
  70. 'is_primary' => false,
  71. ]);
  72. }
  73. }