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.

public function create(): Response
{
    return Inertia::render('CreateJob', [
        'mechanics' => User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get(),
        'consultants' => User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get(),
	]);
}

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.

public function create(): Response
{
    return Inertia::render('CreateJob', [
        'mechanics' => function () {
            return User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get();
        },
        'consultants' => function () {
            return User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get();
        },
    ]);
}

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.

public function create(): Response
{
    return Inertia::render('CreateJob', [
        'mechanics' => Inertia::lazy(function () {
            return User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get();
        }),
        'consultants' => Inertia::lazy(function () {
            return User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get();
        })
    ]);
}

Here are all versions again:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Inertia\Inertia;
use Inertia\Response;

class JobsController extends Controller
{
    public function create(): Response
    {
        return Inertia::render('CreateJob', [
            // always executed
            'mechanics' => User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get(),
            'consultants' => User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get(),

            // executed on first load and partial requests when asked for
            'mechanics' => function () {
                return User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get();
            },
            'consultants' => function () {
                return User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get();
            },

            // only on partial requests when asked for
            'mechanics' => Inertia::lazy(function () {
                return User::where('type', 'mechanic')->filter(request()->only('term'))->limit(15)->get();
            }),
            'consultants' => Inertia::lazy(function () {
                return User::where('type', 'consultant')->filter(request()->only('term'))->limit(15)->get();
            })
        ]);
    }
}

You must have an account to view the code for this episode.