Select Component inside helper class is not updated

I have a Select Component for a Form that is inside a helper class. This Select Component is depending on some other values from the Form itself. If I put the Select Component on the Form, use live() on the other components, all works. As soon I move the Select Component to a helper class, it doesn't update anymore. I then first have to save the Form and reload the page.
Select::make('client_id')
->live()
->helperText('De organisatie die deze vacature plaatst')
->options(...)
->searchable()
->preload(),
Select::make('client_id')
->live()
->helperText('De organisatie die deze vacature plaatst')
->options(...)
->searchable()
->preload(),
Updated Select:
Forms\Components\Group::make(fn (Get $get) => [
TemplateHelper::getTemplates($get('client_id'), null, 'null'),
]),
Forms\Components\Group::make(fn (Get $get) => [
TemplateHelper::getTemplates($get('client_id'), null, 'null'),
]),
If I use the Select Component right away, all works fine. Is this by design, or do I forget something?
6 Replies
Patrick Boivin
Patrick Boivin16mo ago
What's happening inside of TemplateHelper::getTemplates() ?
Daniel Plomp
Daniel PlompOP16mo ago
It is just returning a Select component. The query for the Options is a bit complex, so therefore AND for DRY purposes I decided to use a helper.
Patrick Boivin
Patrick Boivin16mo ago
Sorry, I think I don't understand what you're trying to do. Why do you need to $get() the value and pass it in to the helper that is generating the field?
Daniel Plomp
Daniel PlompOP16mo ago
This is my helper. Maybe that makes more sense?
class TemplateHelper
{
public static function getTemplateOptions($clientId = null, $contentTypeId = null, $contentTypeCategoryId = null): Collection
{
$query = Template::query()
->select(DB::raw("CONCAT(templates.name, ' (', templates.code, ')') as display_name"), 'templates.id')
->leftJoin('content_types', 'templates.content_type_id', '=', 'content_types.id')
->leftJoin('content_type_categories', 'content_types.content_type_category_id', '=', 'content_type_categories.id')
->orderBy('templates.name');

if ($clientId) { $query->where('client_id', $clientId);}

// Add filter for contentTypeId if provided
if ($contentTypeId) { $query->where('content_type_id', $contentTypeId); }

// Add filter for contentTypeCategory if provided
if ($contentTypeCategoryId) { $query->where('content_type_categories.id', $contentTypeCategoryId); }

return $query->pluck('display_name', 'id');
}

public static function getTemplates($clientId = null, $contentTypeId = null, $contentTypeCategoryName = null)
{
return Select::make('template_id')
->allowHtml()
->columnSpanFull()
->label(__('strings.models.templates.single'))
->searchable()
->preload()
->suffixAction(
Action::make('edit_template')
->icon('heroicon-o-eye')
->url(function ($state) {
if ($state) {
$template = Template::find($state);

return TemplateResource::getUrl('edit', ['record' => $template]);
}

return null;
})
)
->options(self::getOptions($clientId, $contentTypeId, $contentTypeCategoryName));
}
}
class TemplateHelper
{
public static function getTemplateOptions($clientId = null, $contentTypeId = null, $contentTypeCategoryId = null): Collection
{
$query = Template::query()
->select(DB::raw("CONCAT(templates.name, ' (', templates.code, ')') as display_name"), 'templates.id')
->leftJoin('content_types', 'templates.content_type_id', '=', 'content_types.id')
->leftJoin('content_type_categories', 'content_types.content_type_category_id', '=', 'content_type_categories.id')
->orderBy('templates.name');

if ($clientId) { $query->where('client_id', $clientId);}

// Add filter for contentTypeId if provided
if ($contentTypeId) { $query->where('content_type_id', $contentTypeId); }

// Add filter for contentTypeCategory if provided
if ($contentTypeCategoryId) { $query->where('content_type_categories.id', $contentTypeCategoryId); }

return $query->pluck('display_name', 'id');
}

public static function getTemplates($clientId = null, $contentTypeId = null, $contentTypeCategoryName = null)
{
return Select::make('template_id')
->allowHtml()
->columnSpanFull()
->label(__('strings.models.templates.single'))
->searchable()
->preload()
->suffixAction(
Action::make('edit_template')
->icon('heroicon-o-eye')
->url(function ($state) {
if ($state) {
$template = Template::find($state);

return TemplateResource::getUrl('edit', ['record' => $template]);
}

return null;
})
)
->options(self::getOptions($clientId, $contentTypeId, $contentTypeCategoryName));
}
}
Patrick Boivin
Patrick Boivin16mo ago
Not sure... I see getTemplateOptions() vs. self::getOptions()
Daniel Plomp
Daniel PlompOP16mo ago
So, I in my Form I use: getTemplates. This returns the Select Component. That works fine. I made the getTemplateOptions public, since I also tried to just get the options() from the helper. This works also and is what I currently use:
Select::make('template_id')
->allowHtml()
->columnSpanFull()
->label(__('strings.models.templates.single'))
->searchable()
->preload()
->suffixAction(
Action::make('edit_template')
->icon('heroicon-o-eye')
->url(function ($state) {
if ($state) {
$template = Template::find($state);

return TemplateResource::getUrl('edit', ['record' => $template]);
}

return null;
})
)
->options(function (Get $get) {
$contentTypeId = $get('content_type_id');

return TemplateHelper::getTemplateOptions(null, $contentTypeId, null);
}),
Select::make('template_id')
->allowHtml()
->columnSpanFull()
->label(__('strings.models.templates.single'))
->searchable()
->preload()
->suffixAction(
Action::make('edit_template')
->icon('heroicon-o-eye')
->url(function ($state) {
if ($state) {
$template = Template::find($state);

return TemplateResource::getUrl('edit', ['record' => $template]);
}

return null;
})
)
->options(function (Get $get) {
$contentTypeId = $get('content_type_id');

return TemplateHelper::getTemplateOptions(null, $contentTypeId, null);
}),
So, in the last approach I use, the Select Component is on the Form itself. If I then update e.g. the Client or the ContentType Select Components, this Select is being updated. If I move this to the helper class, it isn't updated anymore.
Want results from more Discord servers?
Add your server