How do I get this Registration Page to work?

I want to create a company immediately after registration. This should work, but the issue is I get an error: "email is already taken for the email field". I then refresh and get taken to the Tenant Registration page. So, Filament cancels out my DB Transaction somehow.. Even though the user is created just not the company.
class Register extends FilamentRegister
{
public function form(Form $form): Form
{
return $form
->schema([
$this->getNameFormComponent(),
$this->getEmailFormComponent(),
$this->getPasswordFormComponent(),
$this->getPasswordConfirmationFormComponent(),
])
->statePath('data')
->model(User::class);
}

public function register(): ?RegistrationResponse
{
parent::register();

$user = $this->createUser();

event(new Registered($user));

Filament::auth()->login($user);

session()->regenerate();

return app(RegistrationResponse::class);
}

public function createUser(): User
{
$data = $this->form->getState();

return DB::transaction(function () use ($data) {
return tap(User::create($data), function (User $user) {
$this->createCompany($user);
});
});
}

protected function createCompany(User $user): void
{
$user->ownedCompanies()->save(Company::forceCreate([
'user_id' => $user->id,
'name' => explode(' ', $user->name, 2)[0]."'s Company",
'personal_company' => true,
]));
}
}
class Register extends FilamentRegister
{
public function form(Form $form): Form
{
return $form
->schema([
$this->getNameFormComponent(),
$this->getEmailFormComponent(),
$this->getPasswordFormComponent(),
$this->getPasswordConfirmationFormComponent(),
])
->statePath('data')
->model(User::class);
}

public function register(): ?RegistrationResponse
{
parent::register();

$user = $this->createUser();

event(new Registered($user));

Filament::auth()->login($user);

session()->regenerate();

return app(RegistrationResponse::class);
}

public function createUser(): User
{
$data = $this->form->getState();

return DB::transaction(function () use ($data) {
return tap(User::create($data), function (User $user) {
$this->createCompany($user);
});
});
}

protected function createCompany(User $user): void
{
$user->ownedCompanies()->save(Company::forceCreate([
'user_id' => $user->id,
'name' => explode(' ', $user->name, 2)[0]."'s Company",
'personal_company' => true,
]));
}
}
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server