Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CreateNewUser.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Actions\FilamentCompanies;
  3. use App\Models\{Company, User};
  4. use Illuminate\Support\Facades\{DB, Hash, Validator};
  5. use Wallo\FilamentCompanies\Contracts\CreatesNewUsers;
  6. use Wallo\FilamentCompanies\Features;
  7. class CreateNewUser implements CreatesNewUsers
  8. {
  9. /**
  10. * Create a newly registered user.
  11. *
  12. * @param array<string, string> $input
  13. */
  14. public function create(array $input): User
  15. {
  16. Validator::make($input, [
  17. 'name' => ['required', 'string', 'max:255'],
  18. 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
  19. 'password' => ['required', 'string', 'min:8', 'confirmed'],
  20. 'terms' => Features::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
  21. ])->validate();
  22. return DB::transaction(function () use ($input) {
  23. return tap(User::create([
  24. 'name' => $input['name'],
  25. 'email' => $input['email'],
  26. 'password' => Hash::make($input['password']),
  27. ]), function (User $user) {
  28. $this->createCompany($user);
  29. });
  30. });
  31. }
  32. /**
  33. * Create a personal company for the user.
  34. */
  35. protected function createCompany(User $user): void
  36. {
  37. $user->ownedCompanies()->save(Company::forceCreate([
  38. 'user_id' => $user->id,
  39. 'name' => explode(' ', $user->name, 2)[0] . "'s Company",
  40. 'personal_company' => true,
  41. ]));
  42. }
  43. }