Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Argolian's avatar

Filament v3.x creating fields from array

I have a form and an array and am trying to create toggle fields for each item in the array like so:

Group::make([
                    Fieldset::make('Available New Functions')->schema([
                        foreach ($newFunctionsArray as $key => $value) {
                            Toggle::make($value)
                                ->label($value)
                                ->default(false),
                        }
                    ])
                ])->columns(5)

However, I get an

syntax error, unexpected token "foreach", expecting "]"

All searches I've found suggest this is possible but I haven't found a method yet that works.

Is there a way to do this? Or must I manually create each field?

0 likes
3 replies
jaseofspades88's avatar
Level 51

Instead of the array with the foreach, which is syntactically flawed, try the following...

array_map(
    fn ($value) => Toggle::make($value)
        ->label($value)
        ->default(false),
    $newFunctionsArray
)

...this will return an array of toggle fields. Or you could use a collection approach, which is cleaner...

    collect($newFunctionsArray)
        ->map(
            fn ($value) => Toggle::make($value)
                ->label($value)
                ->default(false)
        )
		->toArray();
1 like
omarka's avatar

Hello i'm interested in the solution as i want to display TexFields from config('traslation.locale') array

collect(Config::get("translation.locales")) ->map( fn ($value) => TextInput::make($value."name") ->label($value) ->default(false) ) ->toArray(); but when i put this code in the Schema of form function of a Resource in the panel i get this Error Filament\Forms\ComponentContainer::Filament\Forms\Concerns\{closure}(): Argument #1 ($component) must be of type Filament\Forms\Components\Component, array given

Any suggestions on where to place this code ? Thanks

Please or to participate in this conversation.