From 4190df3b1b842d60acc42723946aa8a8906659ff Mon Sep 17 00:00:00 2001 From: Cameron Wilby Date: Fri, 7 May 2021 04:02:41 -0700 Subject: [PATCH] Add performance tips to README.md (#87) --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index 49944e7..05f015b 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,43 @@ If you do use nova-translatable and would like to return the translated name add } ``` +## Performance Tips + +When attaching this field to a resource, you may include the field relation in the `$with` property for that resource to prevent n+1 issues when loading an index page for that resource. +```php +class Company extends Resource +{ + public static $with = []; +} + +class Department extends Resource +{ + public static $with = ['company']; +} + +class Location extends Resource +{ + public static $with = ['department', 'company']; +} +``` + +You may also choose to cache your top-level model to reduce the number of queries made to the database for each row in an index. +```php +NovaBelongsToDepend::make('Company') + ->options(Cache::remember( + 'companies', + 60, + function () { + return Company::all(); + } + )), +NovaBelongsToDepend::make('Department') + ->dependsOn('Company') + ->optionsResolve(function($company) { + return $company->departments; + }) +``` + ## Sample [Demo Project](https://github.com/orlyapps/laravel-nova-demo)