F
Filament8mo ago
Tieme

Register Form with Relations

Hi All, I am making a custome register form just like the documents (https://filamentphp.com/docs/3.x/panels/users#customizing-the-authentication-features) Here is my complete code for the register form (https://gist.github.com/sitenzo/e30e85cabad4020097eed43c3ca51fad) I want to create a new user with relations on register, only the user is created without any relationship records. This is my user model
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, HasRoles, Notifiable, SoftDeletes,TwoFactorAuthenticatable;

public function address(): hasOne
{
return $this->hasOne(Address::class);
}

public function phone()
{
return $this->hasMany(Phone::class);
}
}
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, HasRoles, Notifiable, SoftDeletes,TwoFactorAuthenticatable;

public function address(): hasOne
{
return $this->hasOne(Address::class);
}

public function phone()
{
return $this->hasMany(Phone::class);
}
}
Does anyone know if this is possible and if so, what am i overlooking? Thanks for the support, if you need anymore information i can provide it.
Solution:
If anyone came across the same issue, the solution is as follow: Add the parent function 'register' the the class ```php...
Jump to solution
2 Replies
Tieme
TiemeOP8mo ago
Up up?
Solution
Tieme
Tieme8mo ago
If anyone came across the same issue, the solution is as follow: Add the parent function 'register' the the class
public function register(): ?RegistrationResponse
{
try {
$this->rateLimit(2);
} catch (TooManyRequestsException $exception) {
Notification::make()
->title(__('filament-panels::pages/auth/register.notifications.throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/register.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/register.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]) : null)
->danger()
->send();

return null;
}

$user = $this->wrapInDatabaseTransaction(function () {
$data = $this->form->getState();

return $this->getUserModel()::create($data);
});

event(new Registered($user));

$this->sendEmailVerificationNotification($user);

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

session()->regenerate();

return app(RegistrationResponse::class);
}
public function register(): ?RegistrationResponse
{
try {
$this->rateLimit(2);
} catch (TooManyRequestsException $exception) {
Notification::make()
->title(__('filament-panels::pages/auth/register.notifications.throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/register.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/register.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]) : null)
->danger()
->send();

return null;
}

$user = $this->wrapInDatabaseTransaction(function () {
$data = $this->form->getState();

return $this->getUserModel()::create($data);
});

event(new Registered($user));

$this->sendEmailVerificationNotification($user);

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

session()->regenerate();

return app(RegistrationResponse::class);
}
And change following lines
$user = $this->wrapInDatabaseTransaction(function () {
$data = $this->form->getState();

return $this->getUserModel()::create($data);
});
$user = $this->wrapInDatabaseTransaction(function () {
$data = $this->form->getState();

return $this->getUserModel()::create($data);
});
to
$user = $this->wrapInDatabaseTransaction(function () {
$data = $this->form->getState();

$record = $this->getUserModel()::create($data);

$this->form->model($record)->saveRelationships();

return $record;
});
$user = $this->wrapInDatabaseTransaction(function () {
$data = $this->form->getState();

$record = $this->getUserModel()::create($data);

$this->form->model($record)->saveRelationships();

return $record;
});
Want results from more Discord servers?
Add your server