phplaravelvue.jsinertiajslaravel-fortify

Passing properties to an Inertia Vue page for Laravel Fortify's register view


I'm working on a web app that's using Laravel 10, Vue 3, and Inertia. (I'm new to all of these, btw) Laravel is using Fortify for a lot of features, including new user registration. I've been tasked with changing the registration page to display some things from the database. As far as I can tell, the usual approach to this would be to add some properties on the Vue page, and then populate those properties via Inertia somewhere in the Laravel app (e.g. a controller).

The challenge I'm encountering right now is that Fortify currently sets up the "register" view (implemented in the Jetstream service provider; see https://github.com/laravel/jetstream/blob/4.x/src/JetstreamServiceProvider.php#L210), pointing to the Auth/Register Vue page, but provides no way to add properties to the Inertia rendering (as far as I can tell).

So the questions are:


Solution

  • tl;dr: I can make my own call to Fortify::registerView() in app\Providers\JetstreamServiceProvider without issue.

    I originally took the approach of making a subsequent ajax call using axios to fetch the necessary data for the Register page. However, I was tasked with adding yet another dynamic part to the Registration page, and I didn't want to have a pattern of making a bunch of ajax calls to populate portions of the page. With the help of Copilot, I found that I can make my own call to Fortify::registerView() in app\Providers\JetstreamServiceProvider and it will not conflict with the default implementation (linked to in my original question). In my case, my implementation looks like this (only including the applicable parts):

    namespace App\Providers;
    
    use App\Models\Plan;
    use Illuminate\Support\ServiceProvider;
    use Inertia\Inertia;
    use Laravel\Fortify\Fortify;
    
    class JetstreamServiceProvider extends ServiceProvider
    {
        public function boot(): void
        {
            Fortify::registerView(function () {
                $plans = Plan::where('status', 1)->get();
    
                return Inertia::render('Auth/Register', [
                    'plans' => $plans,
                ]);
            });
        }
    }