14

I want to share something I figured out since there's not much info out there (that I couldn't find). Laravel 8 with Jetstream Inertia has a few shared objects, like user, current route... You can access them in your components using the $page variable. I needed to add a menu array as a global variable but could not figure it out, even after finding some info on the the official Inertia documentation. It's just different in Laravel Jetstream.

It wasn't until I found Laravel Jetstream's middleware for shared data (ShareInertiaData) that I figured out how to do it.

Here's it is:

  1. Create a middleware in the app/Http/Middleware.php. I called mine ShareInertiaCustomData.
<?php

namespace App\Http\Middleware;

use Inertia\Inertia;

class ShareInertiaCustomData
{
    public function handle($request, $next)
    {
        Inertia::share([
            'menu' => config('menu'),
        ]);

        return $next($request);
    }
}
  1. Place it in the app/Http/Kernel.php
    protected $middlewareGroups = [
        'web' => [
            ...
            \App\Http\Middleware\ShareInertiaCustomData::class,
        ],
    ];

I hope that helps and no one else will have to spend hours trying to figure this out.

phoenix
  • 1,629
  • 20
  • 11
  • 2
    Thanks @phoenix for posting this. I'd spent an hour trying to figure this out! There's a definite gap in the docs between Jetstream and Inertia over this. It might be a good idea to post this as a question and then post an answer to your question following the usual stack overflow standard though, as the moderators might not like the way it is at the moment and it'd be a shame if they delete it this is very important info for anyone wanting to do the same thing. – Joseph Nov 22 '20 at 16:29

2 Answers2

5

Thanks for sharing this! While going through your steps I identified the class \App\Http\Middleware\HandleInertiaRequests, which is the right place to enter own page props. You may use the "share" method to add your properties. This is also explained in the Inertia documentation about Shared Data. Please see an example below.

/**
 * Defines the props that are shared by default.
 *
 * @see https://inertiajs.com/shared-data
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function share(Request $request)
{
    return array_merge(parent::share($request), [
        'current_topic' => Auth::user() ? Auth::user()->currentTeam->currentTopic : null
    ]);
}

}

Marco
  • 89
  • 2
  • 6
  • Thanks. It turns out that my issue was quickly resolved days after I submitted this tutorial. Check out what @ben.horvath's answer – phoenix Feb 24 '21 at 22:12
  • You are absolutely right. Ben already mentioned the share function. – Marco Feb 25 '21 at 05:32
1

Thanks to the guys at the official Discord channel, I found out that I had the same problem because I had an older version of the inertia-laravel package which didn't include a share function.

Make sure you use at least v0.3.0 where it is included.

  • 2
    As per https://github.com/laravel/jetstream/pull/371 I think you'd need to use the (yet unreleased) jetstream 2.x branch for inertia-laravel 0.3.0 to work properly. For the currently released Jetstream 1.x branch we are stuck with inertia-laravel 0.2.4 – Joseph Nov 23 '20 at 06:56