@@ -25,7 +25,7 @@ We assume you've already got the Inertia adapter for Laravel installed.
25
25
Controllers in Laravel are meant to be slim. We have Form Requests to extract our the validation & authorization logic and
26
26
our display logic is in our views, so why do we still insist on making our controllers handle fetching the data for those views?
27
27
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.
29
29
30
30
Data providers extract the data composition for your Inertia views into their own classes. Inertia data providers may prove particularly
31
31
useful if multiple routes or controllers within your application always needs a particular piece of data.
@@ -65,15 +65,36 @@ class DemoController extends Controller
65
65
Data providers can live anywhere, but we' ll use ` App/Http/DataProviders` for this example.
66
66
67
67
The 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
+ ` ` `
68
88
69
- A ** kitchen sink** data provider might look like this:
89
+ A full ** kitchen sink** data provider might look like this:
70
90
71
91
` ` ` php
72
92
< ? php
73
93
declare(strict_types=1);
74
94
75
95
namespace App\H ttp\D ataProviders;
76
96
97
+ use Inertia\D eferProp;
77
98
use Inertia\L azyProp;
78
99
use App\S ervices\I njectedDependency;
79
100
use Webfox\I nertiaDataProviders\D ataProvider;
@@ -116,16 +137,16 @@ class DemoDataProvider extends DataProvider
116
137
}
117
138
118
139
/*
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
123
144
*/
124
- public function quickLazyExample (): Closure
145
+ public function deferredExample (): DeferProp
125
146
{
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
+ ) ;
129
150
}
130
151
131
152
/*
@@ -141,6 +162,19 @@ class DemoDataProvider extends DataProvider
141
162
);
142
163
}
143
164
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
+
144
178
/*
145
179
* ` protected` and ` private` methods are not available to the page
146
180
* /
0 commit comments