techouse / select-auto-complete

An auto-completing Laravel Nova search field
MIT License
31 stars 16 forks source link

N+1 Query Detected #7

Closed shineirvin closed 5 years ago

shineirvin commented 5 years ago
    public function fields()
    {
        return [
            Select::make(__('Person'), 'person')
                  ->options(\App\Person::all()->mapWithKeys(function ($person) {
                      return [$person->id => $person->name];
                  }))
                  ->displayUsingLabels(),
     }

\App\Person::all() is causing N+1 Query, How do I prevent it ?

techouse commented 5 years ago

This is more of an ORM issue than a plugin issue. You should either refine your query and make it more specific, not necessarily faster though, or in case you need the whole table (hence calling ::all()) I guess you could cache the result and read from cache. Whenever the users table or any other list you'd like to use gets updated also update the cached version.