#CheckboxList description with relationships

12 messages · Page 1 of 1 (latest)

pallid oyster
#

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');

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);
}

So, how can I display correct description on checkboxlist descriptions without duplicate query and thanks in advance

winged tangleBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

frozen spire
pallid oyster
#

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;
})

but would like to know better options

pallid oyster
frozen spire
pallid oyster
#

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

vast axle
pallid oyster
#

Try this approach and I end up to same n+1 (for cache).. this is how I try

On Create page

#[Computed]
public function getTechnologyDescriptions()
{
    return Cache::remember('abc_technologies', 300, function () {
        return Technology::query()->pluck('detail', 'id')->toArray();
    });
}

calling from Form like ->descriptions(fn ($livewire) => $livewire->getTechnologyDescriptions())

vast axle
pallid oyster
#

Maybe, will check more and choose one, hope we can load along with relationships and display

cobalt bluff
#

Try accessing the computed property like a normal property:$livewire->getTechnologyDescriptions instead of $livewire->getTechnologyDescriptions()