F
Filament22h ago
Vp

CheckboxList description with relationships

I have table called technologies which has id, name, description and I am displaying this using checkboxlist like below
CheckboxList::make('technologies')
->required()
->relationship(titleAttribute: 'name');
CheckboxList::make('technologies')
->required()
->relationship(titleAttribute: 'name');
Since CheckboxList can also display description, I want it to display my technologies description as well, I try like below ->descriptions(fn() => Technologies::pluck('description', 'id')->toArray()); And yes this introduce duplicate query, and I cannot use fn(Model $record) because of relationships (maybe) I call this from UserForm, and users -> technologies relationships is
public function technologies(): BelongsToMany
{
return $this->belongsToMany(Technology::class);
}
public function technologies(): BelongsToMany
{
return $this->belongsToMany(Technology::class);
}
So, how can I display correct description on checkboxlist descriptions without duplicate query and thanks in advance
4 Replies
LeandroFerreira
LeandroFerreira20h ago
I haven't tried it yet but would removing one query make a big difference to your page?
Vp
VpOP16h ago
I have around 3-4 relations like this for where I want to use, and now I prevent it like this for duplicate query and it works
->descriptions(function () {
static $technologies = null;

if ($technologies === null) {
$technologies = Technology::pluck('detail', 'id')->toArray();
}

return $technologies;
})
->descriptions(function () {
static $technologies = null;

if ($technologies === null) {
$technologies = Technology::pluck('detail', 'id')->toArray();
}

return $technologies;
})
but would like to know better options the reasons I had for description is to display in brief and yes it does make difference
LeandroFerreira
LeandroFerreira16h ago
so I would use a computed method in page/pages
#[Computed]
public function getTechnologyDescriptions(): array
{
return Technology::pluck('detail', 'id')->toArray();
}
#[Computed]
public function getTechnologyDescriptions(): array
{
return Technology::pluck('detail', 'id')->toArray();
}
->descriptions(fn (Page $livewire): array => $livewire->getTechnologyDescriptions())
->descriptions(fn (Page $livewire): array => $livewire->getTechnologyDescriptions())
Laravel
Computed Properties | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Vp
VpOP8h ago
Unfortunately this doesn't work for me (maybe I am noob). These checkbox list is display inside panel, and when I try this n+1 is still happening

Did you find this page helpful?