Urbanist
Urbanist
FFilament
Created by Urbanist on 5/8/2025 in #❓┊help
Variable inside forms Action->action are stuck to first iteration, when I'm creating it in foreach
No description
8 replies
FFilament
Created by Urbanist on 5/8/2025 in #❓┊help
Variable inside forms Action->action are stuck to first iteration, when I'm creating it in foreach
// 1) Combine labels & icons in one array:
$elementTypes = [
'one_image' => ['label' => 'One Image', 'icon' => 'heroicon-m-photo'],
'text' => ['label' => 'Text', 'icon' => 'heroicon-m-document-text'],
'quote' => ['label' => 'Quote', 'icon' => 'heroicon-m-chat-bubble-bottom-center-text'],
'h2' => ['label' => 'Heading 2', 'icon' => 'heroicon-m-hashtag'],// and others...
];

$actions = [];

foreach($elementTypes as $key => $type)
{
$actions[] = Action::make('addElement_{$key}')
->label($elementTypes[$key]['label'])
->icon(fn (Get $get) => $elementTypes[$key]['icon'] ?? 'heroicon-m-plus')
->action(function (Set $set, Get $get) use ($elementTypes, $key) {
$items = $get('elements') ?? [];
$type = $key;
$items[] = [
'type' => $type,
'value' => null,
'sort' => count($items),
];
$set('elements', $items);
$set('newElementType', null);
$key = null;
});
}
// 1) Combine labels & icons in one array:
$elementTypes = [
'one_image' => ['label' => 'One Image', 'icon' => 'heroicon-m-photo'],
'text' => ['label' => 'Text', 'icon' => 'heroicon-m-document-text'],
'quote' => ['label' => 'Quote', 'icon' => 'heroicon-m-chat-bubble-bottom-center-text'],
'h2' => ['label' => 'Heading 2', 'icon' => 'heroicon-m-hashtag'],// and others...
];

$actions = [];

foreach($elementTypes as $key => $type)
{
$actions[] = Action::make('addElement_{$key}')
->label($elementTypes[$key]['label'])
->icon(fn (Get $get) => $elementTypes[$key]['icon'] ?? 'heroicon-m-plus')
->action(function (Set $set, Get $get) use ($elementTypes, $key) {
$items = $get('elements') ?? [];
$type = $key;
$items[] = [
'type' => $type,
'value' => null,
'sort' => count($items),
];
$set('elements', $items);
$set('newElementType', null);
$key = null;
});
}
8 replies
FFilament
Created by Urbanist on 5/8/2025 in #❓┊help
Variable inside forms Action->action are stuck to first iteration, when I'm creating it in foreach
No description
8 replies