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

UserFactory.php 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Database\Factories;
  3. use App\Models\Company;
  4. use App\Models\Setting\CompanyDefault;
  5. use App\Models\Setting\CompanyProfile;
  6. use App\Models\User;
  7. use Illuminate\Database\Eloquent\Factories\Factory;
  8. use Illuminate\Support\Facades\Hash;
  9. use Illuminate\Support\Str;
  10. use Wallo\FilamentCompanies\FilamentCompanies;
  11. class UserFactory extends Factory
  12. {
  13. /**
  14. * The name of the factory's corresponding model.
  15. *
  16. * @var string
  17. */
  18. protected $model = User::class;
  19. /**
  20. * The current password being used by the factory.
  21. */
  22. protected static ?string $password = null;
  23. /**
  24. * Define the model's default state.
  25. *
  26. * @return array<string, mixed>
  27. */
  28. public function definition(): array
  29. {
  30. return [
  31. 'name' => fake()->name(),
  32. 'email' => fake()->unique()->safeEmail(),
  33. 'email_verified_at' => now(),
  34. 'password' => static::$password ??= Hash::make('password'),
  35. 'remember_token' => Str::random(10),
  36. 'profile_photo_path' => null,
  37. 'current_company_id' => null,
  38. ];
  39. }
  40. /**
  41. * Indicate that the model's email address should be unverified.
  42. */
  43. public function unverified(): static
  44. {
  45. return $this->state(static fn (array $attributes) => [
  46. 'email_verified_at' => null,
  47. ]);
  48. }
  49. /**
  50. * Indicate that the user should have a personal company.
  51. */
  52. public function withPersonalCompany(?callable $callback = null): static
  53. {
  54. if (! FilamentCompanies::hasCompanyFeatures()) {
  55. return $this->state([]);
  56. }
  57. $countryCode = $this->faker->countryCode;
  58. return $this->afterCreating(function (User $user) use ($countryCode, $callback) {
  59. Company::factory()
  60. ->state(static fn (array $attributes, User $user) => [
  61. 'name' => $user->name . '\'s Company',
  62. 'user_id' => $user->id,
  63. 'personal_company' => true,
  64. ])
  65. ->has(CompanyProfile::factory()->withCountry($countryCode), 'profile')
  66. ->afterCreating(function (Company $company) use ($user, $countryCode) {
  67. CompanyDefault::factory()->withDefault($user, $company, $countryCode)->create();
  68. })
  69. ->when(is_callable($callback), $callback)
  70. ->create();
  71. });
  72. }
  73. }