You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserFactory.php 2.0KB

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