F
Filament11mo ago
Dwayne

Assign an array to a Forms\Components\Select

How can I assign an array to a Forms\Components\Select? After executing the Action 'refresh_status', the Select options should be set with $statuses. $statuses has the value: [ "pending" => "Wachtend op betaling" "processing" => "In behandeling" "on-hold" => "In de wacht" "completed" => "Afgerond" "cancelled" => "Geannuleerd" "refunded" => "Terugbetaald" "failed" => "Mislukt" "checkout-draft" => "Concept" ]
Solution:
It works! Having
php public array $importableStatuses = [];
php public array $importableStatuses = [];
defined in the ListSuppliers makes it work. Thank you so so much for the help!...
Jump to solution
23 Replies
Dwayne
DwayneOP11mo ago
Dwayne
DwayneOP11mo ago
Anyone?
Dennis Koch
Dennis Koch11mo ago
You cannot set the values from somewhere else. But you can dynamically set them from the closure:
->options(function ($livewire) =>
if ($livewire->some_property) {
return [your_new_statuses];
}

return [all_statuses];
}
->options(function ($livewire) =>
if ($livewire->some_property) {
return [your_new_statuses];
}

return [all_statuses];
}
Dwayne
DwayneOP11mo ago
Thank you @Dennis Koch for your response! So, it's not possible in any way to set the Select options via the hintAction? We need to show a link to fetch the statuses externally if they don't exist in the database yet.
Dennis Koch
Dennis Koch11mo ago
As I said; You cannot set options from some other place. But you can dynamically change the options from within the closure reacting on external changes. Set a property on the livewire component $loadExternal or similar and load the options based on that
Haiffy
Haiffy11mo ago
A little bit off topic, how do i dinamically add an option to be selected based on certain condition? For example, i have this select form
Forms\Components\Select::make('imuninization')
->native(false)
->multiple()
->searchable()
->options([
'hb0' => 'HB 0',
'bcg' => 'BCG',
'opv0' => 'OPV 0',
]),
Forms\Components\Select::make('imuninization')
->native(false)
->multiple()
->searchable()
->options([
'hb0' => 'HB 0',
'bcg' => 'BCG',
'opv0' => 'OPV 0',
]),
if a baby is 1 day old, then 'hb0' would be selected
Dennis Koch
Dennis Koch11mo ago
If you want to change the value while changing fields, use AgeField::make()->afterStateUpdated(fn (Set $set) => $set('imunization', ['hb0']))
Haiffy
Haiffy11mo ago
thanks, however, i'm looking for way to populate the selected value automatically when opening the form, not when changing any field. this is my temporary solution
Select::make('imunization')
->multiple
->default(function () {
$imunization = [];
// first condition
$days = ... ;

if ($days <= 0) {
$imunization += ['hb0'];
}
// and many other conditions ...

return $imunization;
})
Select::make('imunization')
->multiple
->default(function () {
$imunization = [];
// first condition
$days = ... ;

if ($days <= 0) {
$imunization += ['hb0'];
}
// and many other conditions ...

return $imunization;
})
Dennis Koch
Dennis Koch11mo ago
if a baby is 1 day old, then 'hb0' would be selected
I assumed you wanted to react on form input because of this. If you only want a default when creating your "temporary solutions" is good.
Haiffy
Haiffy11mo ago
alright thanks
Dwayne
DwayneOP11mo ago
I don't quite grasp what you mean, I'm still fairly new to Filament. Do you have an example to help me get started?
Dennis Koch
Dennis Koch11mo ago
I did post an example in my first answer?
Dwayne
DwayneOP11mo ago
Yes but how/where do I need to set the some_property
Dennis Koch
Dennis Koch11mo ago
Wherever you want to trigger the other options to be loaded. In this case in your hintActions
Dwayne
DwayneOP11mo ago
Apologies, I'm not understanding... Here's what I have so far:
Forms\Components\Select::make('data.importable_statuses')
->label('Geautomatiseerde orderverwerking')
->options(fn (): Collection =>
collect(
CompanyShopSupplier::where('supplier_id', $record->id)->first()->companyShop->settings->where('setting_name', 'woocommerce_statusses')->first()->setting_value
)->mapWithKeys(fn ($item, $key) => [
$key => $item
])
)
->multiple()
->hintAction(
Forms\Components\Actions\Action::make('refresh_status')
->label('Statussen vernieuwen')
->action(function () {
$shopConnection = ConnectToShop::connect();
$response = $shopConnection->get('reports/orders/totals');

if ($response) {
$statuses = [];
foreach($response as $result) {
$statuses[$result->slug] = $result->name;
}

$updated = CompanyShopSetting::updateOrCreate([
'setting_name' => 'woocommerce_statusses',
], [
'setting_value' => $statuses
]);

if ($updated) {

// Here we need to update the select field "data.importable_statuses" with the $statuses

}
}
})
)
php
Forms\Components\Select::make('data.importable_statuses')
->label('Geautomatiseerde orderverwerking')
->options(fn (): Collection =>
collect(
CompanyShopSupplier::where('supplier_id', $record->id)->first()->companyShop->settings->where('setting_name', 'woocommerce_statusses')->first()->setting_value
)->mapWithKeys(fn ($item, $key) => [
$key => $item
])
)
->multiple()
->hintAction(
Forms\Components\Actions\Action::make('refresh_status')
->label('Statussen vernieuwen')
->action(function () {
$shopConnection = ConnectToShop::connect();
$response = $shopConnection->get('reports/orders/totals');

if ($response) {
$statuses = [];
foreach($response as $result) {
$statuses[$result->slug] = $result->name;
}

$updated = CompanyShopSetting::updateOrCreate([
'setting_name' => 'woocommerce_statusses',
], [
'setting_value' => $statuses
]);

if ($updated) {

// Here we need to update the select field "data.importable_statuses" with the $statuses

}
}
})
)
php
Dennis Koch
Dennis Koch11mo ago
Something like this:
<?php

// Your EditPage/CreatePage

public array $importableStatuses = [];

// Form
Forms\Components\Select::make('data.importable_statuses')
->label('Geautomatiseerde orderverwerking')
->options(function ($livewire): Collection {
if (filled($livewire->importableStatuses)) {
return collect($livewire->importableStatuses);
}

return collect(
CompanyShopSupplier::where('supplier_id', $record->id)->first()->companyShop->settings->where('setting_name', 'woocommerce_statusses')->first()->setting_value
)->mapWithKeys(fn ($item, $key) => [
$key => $item
]);
})
->multiple()
->hintAction(
Forms\Components\Actions\Action::make('refresh_status')
->label('Statussen vernieuwen')
->action(function ($livewire->) {
$shopConnection = ConnectToShop::connect();
$response = $shopConnection->get('reports/orders/totals');

if ($response) {
$statuses = [];
foreach($response as $result) {
$statuses[$result->slug] = $result->name;
}

$updated = CompanyShopSetting::updateOrCreate([
'setting_name' => 'woocommerce_statusses',
], [
'setting_value' => $statuses
]);

if ($updated) {
$livewire->importableStatuses = $statuses;
}
}
})
)
<?php

// Your EditPage/CreatePage

public array $importableStatuses = [];

// Form
Forms\Components\Select::make('data.importable_statuses')
->label('Geautomatiseerde orderverwerking')
->options(function ($livewire): Collection {
if (filled($livewire->importableStatuses)) {
return collect($livewire->importableStatuses);
}

return collect(
CompanyShopSupplier::where('supplier_id', $record->id)->first()->companyShop->settings->where('setting_name', 'woocommerce_statusses')->first()->setting_value
)->mapWithKeys(fn ($item, $key) => [
$key => $item
]);
})
->multiple()
->hintAction(
Forms\Components\Actions\Action::make('refresh_status')
->label('Statussen vernieuwen')
->action(function ($livewire->) {
$shopConnection = ConnectToShop::connect();
$response = $shopConnection->get('reports/orders/totals');

if ($response) {
$statuses = [];
foreach($response as $result) {
$statuses[$result->slug] = $result->name;
}

$updated = CompanyShopSetting::updateOrCreate([
'setting_name' => 'woocommerce_statusses',
], [
'setting_value' => $statuses
]);

if ($updated) {
$livewire->importableStatuses = $statuses;
}
}
})
)
Dwayne
DwayneOP11mo ago
class SupplierResource extends Resource
{
use SupplierInteraction;

public array $importableStatuses = [];
class SupplierResource extends Resource
{
use SupplierInteraction;

public array $importableStatuses = [];
No description
Dwayne
DwayneOP11mo ago
No description
Dwayne
DwayneOP11mo ago
Maybe more convenient
Dennis Koch
Dennis Koch11mo ago
See the comment. This belongs on the page. Not the resource. Resources aren't Livewire components
Dwayne
DwayneOP11mo ago
Ah, I see! That's unfortunate... Because it's an action in the table on the resource. So I have to make it an CreatePage/EditPage, right?
Dennis Koch
Dennis Koch11mo ago
If it's on the Table that the page for that is the ListPage
Solution
Dwayne
Dwayne11mo ago
It works! Having
php public array $importableStatuses = [];
php public array $importableStatuses = [];
defined in the ListSuppliers makes it work. Thank you so so much for the help!

Did you find this page helpful?