Best practices to improve performance

How can improve performance of resources big form or repeater form or custom page form ... Can I use computed-properties in resources to store some select options that can be limited not big of select options call it on boot function class that come from database also can get it by js https://livewire.laravel.com/docs/computed-properties. We need if you can help me to guide of the best practices ....
5 Replies
toeknee
toeknee4w ago
We need to see the form really. First things first, all selects to be closures to avoid looping Conditions use JS where possible to reduce network requests with the forms, as per visibleJs() on: https://filamentphp.com/docs/4.x/forms/overview Any SQL Queries you can natively cache if they won't change, say 15 minute.
ChrisMerlin
ChrisMerlin4w ago
I managed to get this relational multiple select to run without probing the server. I guess when overriding the saving and loading the component does not request from the server, not sure exactly. Field in TaskForm, to Select one or more Users that are Assigned To the task...
// In TaskForm
Select::make('users')
->label('Assigned To')
->multiple()
->searchable(true)
->nullable()
->default(null)
->options($staffSelectOptions)
->preload() // This helps with loading
// Saving
->saveRelationshipsUsing(function ($component, $state, $record) {
$record->users()->sync($state ?? []);
})
// Loading
->afterStateHydrated(function ($component, $record) {
if ($record) {
$component->state($record->users()->pluck('users.id')->toArray());
}
}),

// In Task Model (relationship using pivot table)
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'task_user', 'task_id', 'user_id', 'id', 'id');
}
// In TaskForm
Select::make('users')
->label('Assigned To')
->multiple()
->searchable(true)
->nullable()
->default(null)
->options($staffSelectOptions)
->preload() // This helps with loading
// Saving
->saveRelationshipsUsing(function ($component, $state, $record) {
$record->users()->sync($state ?? []);
})
// Loading
->afterStateHydrated(function ($component, $record) {
if ($record) {
$component->state($record->users()->pluck('users.id')->toArray());
}
}),

// In Task Model (relationship using pivot table)
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'task_user', 'task_id', 'user_id', 'id', 'id');
}
Arafat Mutahar
Arafat MutaharOP4w ago
The best solution do it by my self store options array in static variable property . Set and get value of property by static method call get method in options functions select::make('field')->options(self::getFieldOptions()) We need by filement to can add custom properties cached during form handling can set it in boot function destroy value of this properties after form completed saved or redirected or by method can set it in class form . Is this feature existing in filament 4
toeknee
toeknee4w ago
It is a livewire component in all filament areas, so you should be able to use computed properties. But also make sure options are enclosed in closures for performance: ->options(fn() => $staffSelectOptions)
Arafat Mutahar
Arafat MutaharOP4w ago
it's calling closure yes . But livewire computed properties not static properties object properties how can get current instance of form class if my class form in schemes folder or current instances of resource or custom page

Did you find this page helpful?