HintAction Adding to Select Element

Hello - I'm wondering if anyone has ever had an issue using a hintAction to add to a select element. Here is the relevant code:
Select::make('meal_id')
->label('Meal')
->required()
->searchable()
->reactive()
->options(function () use ($order) {
$meals = Meal::where('kitchen_id', $order->provider_id)
->get();

$array = [];

foreach ($meals as $meal) {
$array[$meal->id] = $meal->name . ' ($' . $meal->price . ')';
if (isset($meal->servings)) {
$array[$meal->id] .= ' (' . $meal->servings . ')';
}
}

return $array;
})
->hintAction(function ($set) use ($order) {
return Action::make('createCustomRestaurantMeal')
->label('Custom Selection')
->form([
TextInput::make('name')
->required()
->maxLength(100)
])
->action(function ($data) use ($order, $set) {
$meal = Meal::create([
'name' => $data['name'],
'kitchen_id' => $order->provider_id,
'approved' => false,
]);

$set('meal_id', $meal);

$kitchenUsers = $order->getKitchenUsersForNotification();

foreach ($kitchenUsers as $user) {
Notification::make()
->title('New Menu Request')
->body('A custom menu request has been submitted.')
->actions([
ActionsAction::make('view')
->url(route('filament.admin.resources.meal-approvals.index'), shouldOpenInNewTab: true)
])
->sendToDatabase($user);
}
});
})
Select::make('meal_id')
->label('Meal')
->required()
->searchable()
->reactive()
->options(function () use ($order) {
$meals = Meal::where('kitchen_id', $order->provider_id)
->get();

$array = [];

foreach ($meals as $meal) {
$array[$meal->id] = $meal->name . ' ($' . $meal->price . ')';
if (isset($meal->servings)) {
$array[$meal->id] .= ' (' . $meal->servings . ')';
}
}

return $array;
})
->hintAction(function ($set) use ($order) {
return Action::make('createCustomRestaurantMeal')
->label('Custom Selection')
->form([
TextInput::make('name')
->required()
->maxLength(100)
])
->action(function ($data) use ($order, $set) {
$meal = Meal::create([
'name' => $data['name'],
'kitchen_id' => $order->provider_id,
'approved' => false,
]);

$set('meal_id', $meal);

$kitchenUsers = $order->getKitchenUsersForNotification();

foreach ($kitchenUsers as $user) {
Notification::make()
->title('New Menu Request')
->body('A custom menu request has been submitted.')
->actions([
ActionsAction::make('view')
->url(route('filament.admin.resources.meal-approvals.index'), shouldOpenInNewTab: true)
])
->sendToDatabase($user);
}
});
})
Two issues: 1. It is not setting the meal_id after creating the meal in the hintAction 2. After searching for and selecting the meal, it disappears I feel like these two issues are related (#1 might just happen quickly enough that I don't see it selected initially)
Solution:
Of course the answer was in the docs... ->createOptionUsing() needs to return the ID of the newly created model
Jump to solution
3 Replies
TranceCode
TranceCode2w ago
Can you try with this example!
Forms\Components\Select::make('name')
->label('Cuenta Contable')
->options(Account::where('company_id', $tenant->id)->pluck('name', 'name')->toArray())
->preload()
->allowHtml()
->createOptionForm([
Forms\Components\TextInput::make('name')
->label('Nueva cuenta Contable')
->required()
])
->createOptionUsing(function ($data) use ($tenant) {
$data['company_id'] = $tenant->id;
$account = Account::create($data);
return $account->name;
})
->required()
->placeholder('Ingrese una Cuenta contable, ejemplo Gastos Administrativos'),
Forms\Components\Select::make('name')
->label('Cuenta Contable')
->options(Account::where('company_id', $tenant->id)->pluck('name', 'name')->toArray())
->preload()
->allowHtml()
->createOptionForm([
Forms\Components\TextInput::make('name')
->label('Nueva cuenta Contable')
->required()
])
->createOptionUsing(function ($data) use ($tenant) {
$data['company_id'] = $tenant->id;
$account = Account::create($data);
return $account->name;
})
->required()
->placeholder('Ingrese una Cuenta contable, ejemplo Gastos Administrativos'),
No description
No description
bwurtz999
bwurtz999OP2w ago
Trying this now! @TranceCode I never knew about->createOptionForm and ->createOptionUsing, those are great. Thank you! Turns out my issue was here though: $set('meal_id', $meal); Should have been $set('meal_id', $meal->id); But I'm sticking with createOption, I like it better than hintAction in this case Actually I didn't fix the issue...
->createOptionUsing(function ($data, $set) use ($order) {
$meal = Meal::create([
'name' => $data['name'],
'kitchen_id' => $order->provider_id,
'approved' => false,
]);

$set('meal_id', $meal->id);
->createOptionUsing(function ($data, $set) use ($order) {
$meal = Meal::create([
'name' => $data['name'],
'kitchen_id' => $order->provider_id,
'approved' => false,
]);

$set('meal_id', $meal->id);
Does not set the meal_id Any ideas?
Solution
bwurtz999
bwurtz9992w ago
Of course the answer was in the docs... ->createOptionUsing() needs to return the ID of the newly created model
Want results from more Discord servers?
Add your server