InertiaJS data evaluation in Laravel and VueJS applications
Constantin Druc · 20 Feb, 2021
Laravel debugbar package: https://github.com/barryvdh/laravel-debugbar
There are three different ways we can pass data to our InertiaJS responses.
1. By assigning the values directly - values are always evaluated regardless of whether we are dealing with a first page load or a partial request.
1public function create(): Response2{3 return Inertia::render('CreateJob', [4 'mechanics' => User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get(),5 'consultants' => User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get(),6 ]);7}
2. By wrapping and returning the values inside a closure - values are evaluated on first page loads and optionally on partial requests using the only
key of the inertia request options.
1public function create(): Response 2{ 3 return Inertia::render('CreateJob', [ 4 'mechanics' => function () { 5 return User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get(); 6 }, 7 'consultants' => function () { 8 return User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get(); 9 },10 ]);11}
3. By using Inertia's lazy
method - values are never evaluated on first page loads but only optionally, on partial requests using the only
key of the inertia request options.
1public function create(): Response 2{ 3 return Inertia::render('CreateJob', [ 4 'mechanics' => Inertia::lazy(function () { 5 return User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get(); 6 }), 7 'consultants' => Inertia::lazy(function () { 8 return User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get(); 9 })10 ]);11}
Here are all versions again:
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Models\User; 6use Inertia\Inertia; 7use Inertia\Response; 8 9class JobsController extends Controller10{11 public function create(): Response12 {13 return Inertia::render('CreateJob', [14 // always executed15 'mechanics' => User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get(),16 'consultants' => User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get(),17 18 // executed on first load and partial requests when asked for19 'mechanics' => function () {20 return User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get();21 },22 'consultants' => function () {23 return User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get();24 },25 26 // only on partial requests when asked for27 'mechanics' => Inertia::lazy(function () {28 return User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get();29 }),30 'consultants' => Inertia::lazy(function () {31 return User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get();32 })33 ]);34 }35}
-
1Determining active routes05:24
-
2Create a searchable list09:05
-
3Handling regular forms16:41
-
4Toast notifications17:32
-
5Inline forms validation09:15
-
6Authorization08:45
-
7Structuring layouts10:43
-
8Infinite scrolling with InertiaJs and Laravel15:57
-
9Toggling likes13:27
-
10Notifications with Laravel Echo and Pusher in InertiaJS applications (part one)14:47
-
11Realtime Notifications with Laravel Echo and Pusher in InertiaJS applications (part two)09:07
-
12Building a user following / unfollowing system with Laravel and InertiaJS22:39
-
13Changing button labels on hover and InertiaJS progress indicators05:34
-
14Building the home feed and infinite scroll component with Laravel and InertiaJS17:52
-
15Building a form component to post new tweets with Laravel and InertiaJS19:38
-
16Media attachments with Laravel and InertiaJS41:46
-
17Avatar image input component with VueJS, Inertia, and Tailwind CSS14:44
-
18Bugfixing the avatar input component04:16
-
19New InertiaJs form helper - less boilerplate, more awesomeness08:30
-
20InertiaJS scroll management13:34
-
21InertiaJS submitting form arrays and error handling11:46
-
22Create a searchable select field with InertiaJS and vue multiselect13:10
-
InertiaJS data evaluation in Laravel and VueJS applications04:37
-
24Creating a datatable with Laravel and InertiaJS10:58
-
25Customize the default Jetstream view responses in Laravel apps using the Inertia stack03:46
-
26Fuzzy searching with Fuse.js in Laravel & InertiaJS applications10:38
-
27Handling translations in Laravel and InertiaJS applications12:24
-
28Replace auth redirect with modal dialog in InertiaJS & Laravel applications06:49
-
29Prevalidate forms in InertiaJS and Laravel applications16:06
-
30Prevent recently logged out users from seeing private pages when navigating back04:08
-
31InertiaJS frontend validation using Intus11:50
-
32InertiaJS & Vue 3 toast notifications18:33
-
1Determining active routes05:24
-
2Create a searchable list09:05
-
3Handling regular forms16:41
-
4Toast notifications17:32
-
5Inline forms validation09:15
-
6Authorization08:45
-
7Structuring layouts10:43
-
8Infinite scrolling with InertiaJs and Laravel15:57
-
9Toggling likes13:27
-
10Notifications with Laravel Echo and Pusher in InertiaJS applications (part one)14:47
-
11Realtime Notifications with Laravel Echo and Pusher in InertiaJS applications (part two)09:07
-
12Building a user following / unfollowing system with Laravel and InertiaJS22:39
-
13Changing button labels on hover and InertiaJS progress indicators05:34
-
14Building the home feed and infinite scroll component with Laravel and InertiaJS17:52
-
15Building a form component to post new tweets with Laravel and InertiaJS19:38
-
16Media attachments with Laravel and InertiaJS41:46
-
17Avatar image input component with VueJS, Inertia, and Tailwind CSS14:44
-
18Bugfixing the avatar input component04:16
-
19New InertiaJs form helper - less boilerplate, more awesomeness08:30
-
20InertiaJS scroll management13:34
-
21InertiaJS submitting form arrays and error handling11:46
-
22Create a searchable select field with InertiaJS and vue multiselect13:10
-
InertiaJS data evaluation in Laravel and VueJS applications04:37
-
24Creating a datatable with Laravel and InertiaJS10:58
-
25Customize the default Jetstream view responses in Laravel apps using the Inertia stack03:46
-
26Fuzzy searching with Fuse.js in Laravel & InertiaJS applications10:38
-
27Handling translations in Laravel and InertiaJS applications12:24
-
28Replace auth redirect with modal dialog in InertiaJS & Laravel applications06:49
-
29Prevalidate forms in InertiaJS and Laravel applications16:06
-
30Prevent recently logged out users from seeing private pages when navigating back04:08
-
31InertiaJS frontend validation using Intus11:50
-
32InertiaJS & Vue 3 toast notifications18:33