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,
]));
}
}