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