© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•14mo ago•
8 replies
TranceCode

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);
        });
    }
}
image.png
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

How can i register plugins dynamically
FilamentFFilament / ❓┊help
3y ago
How can I hide a field on the edit form?
FilamentFFilament / ❓┊help
3y ago
can i change the logic of login/register?
FilamentFFilament / ❓┊help
2y ago
How to edit the basic url?
FilamentFFilament / ❓┊help
3y ago