andyb300
andyb300

Reputation: 23

Laravel Filament v3 advanced form - Auto populate customer information

I'm building a form in Laravel Filament V3 to process a customer order. I currently have it set up in a wizard. I want to have it auto-populate the address, email, and contact information when you select an existing customer.

I've looked at the documentation, and I think the $get statement is the option I need, but I'm unsure how to implement it.

My code so far is

return $form
        ->schema([
             Forms\Components\Wizard::make([
        Forms\Components\Wizard\Step::make('Customer Details')
            ->schema([
                Forms\Components\Select::make('Company Name')
                    ->required()
                    ->searchable()
                    ->getSearchResultsUsing(fn (string $search): array => Customer::where('Company Name', 'like', "%{$search}%")->limit(50)->pluck('Company Name')->toArray())
                    ->reactive(),
                Forms\Components\TextInput::make('Company Ref'),
                Forms\Components\TextInput::make('Telephone no'),
                Forms\Components\TextInput::make('Mobile No'),
                Forms\Components\TextInput::make('Email')
                ->reactive(),
                Forms\Components\TextInput::make('Contact'),
                Forms\Components\TextInput::make('Artwork Contact'),
                Forms\Components\TextInput::make('Web'),
                    ]),

there are other steps to the wizard, but they want to get Step One working first. Is $get the right option, or is there a better way of doing it

Upvotes: 1

Views: 1963

Answers (1)

yakov wiznitzer
yakov wiznitzer

Reputation: 11

I think that $set would be a better option here. Also, pluck the id as well so you can access the Customer Model later.

return $form
->schema([
    Forms\Components\Wizard::make([
        Forms\Components\Wizard\Step::make('Customer Details')
            ->schema([
                Forms\Components\Select::make('Company Name')
                    ->required()
                    ->searchable()
                    ->getSearchResultsUsing(fn (string $search): array => Customer::where('Company Name', 'like', "%{$search}%")->limit(50)->pluck('id','Company Name')->toArray())
                    ->live()
                    ->afterStateUpdated(function(Set $set, $state) {
                        $customer = Customer::find($state);
                        $set('Company Ref', $customer->CompanyRef);
                        $set('Telephone no', $customer->TelephoneNo);
                        $set('Mobile No', $customer->MobileNo);
                        $set('Email', $customer->Email);
                        $set('Contact', $customer->Contact);
                        $set('Artwork Contact', $customer->ArtworkContact);
                        $set('Web', $customer->Web);
                    }),
                Forms\Components\TextInput::make('Company Ref'),
                Forms\Components\TextInput::make('Telephone no'),
                Forms\Components\TextInput::make('Mobile No'),
                Forms\Components\TextInput::make('Email')
                    ->reactive(),
                Forms\Components\TextInput::make('Contact'),
                Forms\Components\TextInput::make('Artwork Contact'),
                Forms\Components\TextInput::make('Web'),
            ]),
    ])
]);

Upvotes: 1

Related Questions