How can edit the register account?

Hello friends, I am modifying the register a little, I want to save the plan relationship with the user but for some reason it does not take the onSubmit method, I don't know if this method is correct or not?
use App\Models\Plan;
use Filament\Forms\Components\Component;
use Filament\Forms\Components\Select;
use Filament\Pages\Auth\Register as BaseRegister;
use Illuminate\Support\Facades\DB;

class Register extends BaseRegister
{
protected function getForms(): array
{
return [
'form' => $this->form(
$this->makeForm()
->schema([
$this->getNameFormComponent(),
$this->getEmailFormComponent(),
$this->getPasswordFormComponent(),
$this->getPasswordConfirmationFormComponent(),
$this->getPlansFormComponent(),
])
->statePath('data'),
),
];
}

protected function getPlansFormComponent(): Component
{
return Select::make('plans')
->label('Seleccione un plan por 14 días')
->options(
Plan::pluck('name', 'id')->toArray()
)
->required();
}

protected function onSubmit(array $data): void
{
DB::transaction(function () use ($data) {
// Crear el usuario utilizando el método base
$user = $this->getAuth()->create($data);
// Verificar si se seleccionó un plan
if (isset($data['plans']) && $data['plans']) {
// Asignar el plan al usuario recién creado
$user->plans()->attach($data['plans'], [
'start_date' => now(),
'end_date' => now()->addDays(14),
'is_active' => true,
]);
}
// Autenticamos
$this->getAuth()->login($user);
});
}
}
use App\Models\Plan;
use Filament\Forms\Components\Component;
use Filament\Forms\Components\Select;
use Filament\Pages\Auth\Register as BaseRegister;
use Illuminate\Support\Facades\DB;

class Register extends BaseRegister
{
protected function getForms(): array
{
return [
'form' => $this->form(
$this->makeForm()
->schema([
$this->getNameFormComponent(),
$this->getEmailFormComponent(),
$this->getPasswordFormComponent(),
$this->getPasswordConfirmationFormComponent(),
$this->getPlansFormComponent(),
])
->statePath('data'),
),
];
}

protected function getPlansFormComponent(): Component
{
return Select::make('plans')
->label('Seleccione un plan por 14 días')
->options(
Plan::pluck('name', 'id')->toArray()
)
->required();
}

protected function onSubmit(array $data): void
{
DB::transaction(function () use ($data) {
// Crear el usuario utilizando el método base
$user = $this->getAuth()->create($data);
// Verificar si se seleccionó un plan
if (isset($data['plans']) && $data['plans']) {
// Asignar el plan al usuario recién creado
$user->plans()->attach($data['plans'], [
'start_date' => now(),
'end_date' => now()->addDays(14),
'is_active' => true,
]);
}
// Autenticamos
$this->getAuth()->login($user);
});
}
}
No description
6 Replies
TranceCode
TranceCodeOP3w ago
any idea guys?
Julien B. (aka yebor974)
Hi. You can use protected function handleRegistration(array $data): Model to manage you register process and not use onSubmit (does not exist here). For example :
protected function handleRegistration(array $data): Model
{
/** @var User $user */
$user = parent::handleRegistration($data + ['force_renew_password' => false]);
// ... you code here to manage plan
}
protected function handleRegistration(array $data): Model
{
/** @var User $user */
$user = parent::handleRegistration($data + ['force_renew_password' => false]);
// ... you code here to manage plan
}
You can also use relation on your form with the plan.
LeandroFerreira
what is the plan relationship?
TranceCode
TranceCodeOP3w ago
here is the plan relationship in User Model
public function plans(): BelongsToMany
{
return $this->belongsToMany(Plan::class)->withPivot('start_date', 'end_date', 'is_active')->withTimestamps();
}
public function plans(): BelongsToMany
{
return $this->belongsToMany(Plan::class)->withPivot('start_date', 'end_date', 'is_active')->withTimestamps();
}
LeandroFerreira
if you are using belongsToMany, your select should have multiple I think.. if you need to customize the creation process, use
protected function handleRegistration(array $data): Model
{
//... your logic
}
protected function handleRegistration(array $data): Model
{
//... your logic
}
TranceCode
TranceCodeOP3w ago
Than you guys @LeandroFerreira and @Julien B. (aka yebor974) for the tips and answer...

Did you find this page helpful?