@@ -25,7 +25,7 @@ We assume you've already got the Inertia adapter for Laravel installed.
2525Controllers in Laravel are meant to be slim. We have Form Requests to extract our the validation & authorization logic and
2626our display logic is in our views, so why do we still insist on making our controllers handle fetching the data for those views?
2727
28- This is especially evident in Inertia applications due to the introduction of concepts like lazy props.
28+ This is especially evident in Inertia applications due to the introduction of concepts like lazy and deferred props.
2929
3030Data providers extract the data composition for your Inertia views into their own classes. Inertia data providers may prove particularly
3131useful if multiple routes or controllers within your application always needs a particular piece of data.
@@ -65,15 +65,36 @@ class DemoController extends Controller
6565Data providers can live anywhere, but we' ll use ` App/Http/DataProviders` for this example.
6666
6767The simplest data provider is just a class that extends ` DataProvider` , any public methods or properties will be available to the page as data.
68+ ` ` ` php
69+ < ? php
70+ declare(strict_types=1);
71+
72+ namespace App\H ttp\D ataProviders;
73+
74+ use Inertia\L azyProp;
75+ use App\S ervices\I njectedDependency;
76+ use Webfox\I nertiaDataProviders\D ataProvider;
77+
78+ class DemoDataProvider extends DataProvider
79+ {
80+ public string $someData = ' data' ;
81+
82+ public function moreData(): int
83+ {
84+ return time ();
85+ }
86+ }
87+ ` ` `
6888
69- A ** kitchen sink** data provider might look like this:
89+ A full ** kitchen sink** data provider might look like this:
7090
7191` ` ` php
7292< ? php
7393declare(strict_types=1);
7494
7595namespace App\H ttp\D ataProviders;
7696
97+ use Inertia\D eferProp;
7798use Inertia\L azyProp;
7899use App\S ervices\I njectedDependency;
79100use Webfox\I nertiaDataProviders\D ataProvider;
@@ -116,16 +137,16 @@ class DemoDataProvider extends DataProvider
116137 }
117138
118139 /*
119- * If a method returns a ` Closure ` it will be evaluated as a lazy property.
120- * ALWAYS included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed
121- * Additionally the callback methods are resolved through Laravel' s service container, so any parameters will be automatically resolved.
122- * @see https://inertiajs.com/partial-reloads#lazy-data-evaluation
140+ * If a method is typed to return a DeferProp, it will only be evaluated in a deferred request after the page has loaded
141+ * NEVER included on first visit, OPTIONALLY included on partial reloads, ALWAYS evaluated after the page has loaded.
142+ * Additionally the deferred callback methods are resolved through Laravel' s service container, so any parameters will be automatically resolved.
143+ * @see https://inertiajs.com/deferred-props
123144 */
124- public function quickLazyExample (): Closure
145+ public function deferredExample (): DeferProp
125146 {
126- return function(InjectedDependency $example): string {
127- return $example->formatName( $this->demo->user->name);
128- } ;
147+ return Inertia::defer(
148+ fn () => $this->demo->aHeavyCalculation()
149+ ) ;
129150 }
130151
131152 /*
@@ -141,6 +162,19 @@ class DemoDataProvider extends DataProvider
141162 );
142163 }
143164
165+ /*
166+ * If a method returns a `Closure` it will be evaluated as a lazy property.
167+ * ALWAYS included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed
168+ * Additionally the callback methods are resolved through Laravel' s service container, so any parameters will be automatically resolved.
169+ * @see https://inertiajs.com/partial-reloads#lazy-data-evaluation
170+ * /
171+ public function quickLazyExample(): Closure
172+ {
173+ return function(InjectedDependency $example ): string {
174+ return $example -> formatName($this -> demo-> user-> name);
175+ };
176+ }
177+
144178 /*
145179 * ` protected` and ` private` methods are not available to the page
146180 * /
0 commit comments