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.

CreateNewUser.php 1.6KB

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