koss-shtukert / laravel-nova-select2-auto-complete

Laravel Nova Select2 Auto-Complete
https://novapackages.com/packages/koss-shtukert/laravel-nova-select2-auto-complete
MIT License
38 stars 11 forks source link

N+1 Query #29

Closed shineirvin closed 4 years ago

shineirvin commented 5 years ago
public function fields(Request $request) {
        # this causing N+1 Query
        $items = Item::all()->pluck('name', 'id');

        return [
            Select2::make('Items', 'itemList')
                ->sortable()
                ->disableGoogleAnalytics(),                    
        ];
}

How do I prevent the N+1 Query issue ?

koss-shtukert commented 5 years ago

Hi @shineirvin, unfortunately, I don't know how to do that :( Maybe you can use a static list of items...

angauber commented 5 years ago

You could use some cache :

$items = Cache::rememberForever('MY_CACHE_KEY', function () {
    return Item::all()->pluck('name', 'id');
});

return [
    Select2::make('Items', 'itemList')
        ->options($items),
];

Don't forget to forget your Cache on create, delete or update of your item model

ghost commented 5 years ago

@shineirvin can U describe problem?