Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sinnbeck's avatar
Level 102

Inertia - shared data on certain pages

Does anyone have any great ideas on how to add shared data to certain pages only? Something like view composers, but for inertia..

https://inertiajs.com/shared-data

My best idea so far is to have a check on each share with something like if($request->is('admin')) but I fear it will get messy fast. I have considered if it would be possible to split it into two middlewares, but I cant see how..

I have read this discussion, but was hoping for something closer to view composers :) https://github.com/inertiajs/inertia-laravel/issues/74

0 likes
15 replies
bugsysha's avatar

@Sinnbeck then my memory tricked me that it was covered in that video. Sorry. Can you explain what is the problem if you have the data on all pages?

Sinnbeck's avatar
Level 102

@bugsysha I have created an intranet where the whole site is built on inertia. Every page has the same menu where I have shared data like the menu structure.. Not the problem is that I have made a few other types of pages, that is not using the same layout/menu, and therefor does not need any of that stuff. So I am trying to find an elegant way of ensuring that those pieces of shared data isnt used.

bugsysha's avatar

@Sinnbeck there is nothing from stopping you to control it from that middleware as it was shown in the video. Just make it a bit dynamic so it does not get messy.

Sinnbeck's avatar
Level 102

@bugsysha Yeah I have been considering using an array, like with the one in CSRF, to tell it which routes to not add the shared data to. I was just hoping for a cleaner solution :)

bugsysha's avatar

@Sinnbeck would something like this clean things up?

class HandleInertiaRequests extends Middleware
{
    public function share(Request $request)
    {
		$methodName = $this->convertCurrentRouteToMethodName($request);
        return array_merge(parent::share($request), $this->$methodName());
    }
}

Ofc, you can always do the same with classes. Just make classes invokable.

class HandleInertiaRequests extends Middleware
{
    public function share(Request $request)
    {
		$className = $this->convertCurrentRouteToClassName($request);
        return array_merge(parent::share($request), new $className);
    }
}
Sinnbeck's avatar
Level 102

@bugsysha Currently I am trying to make some sort of clever macro to allow me to make my own "view" composer :)

Sinnbeck's avatar
Level 102

Apparently it works if I extract the shared data into a new middleware (in the handle method), which I then add to the routes that are are using the layout :) Easy

3 likes
psalcedodev's avatar

Hi, @Sinnbeck could you show us how you were able to accomplish this with some examples? Thank you

Sinnbeck's avatar
Level 102

@psalcedodev I just made a middleware class with the needed shared data

    public function handle(Request $request, Closure $next)
    {
        Inertia::share([
            'menu'  => function () {
                return (new Menu)->main();
            },
        ]);

        return $next($request);
    }

and used it on a route group

Route::middleware([
    SharedMenu::class, //this is the middleware class
    'auth',
])->group(function () {
4 likes
JD45's avatar

@Sinnbeck Thanks for this idea/suggestion. I was searching around for a better solution than using the default Intertia middleware.

What I was contemplating doing is making use of Pinia, Axios and hitting an API endpoint that way I could use that specific data in whatever component I wanted and not touch Inertia's middleware.

Unfortunately, that led me down a rabbit hole, taking way to much time and resources to implement. I do hope they find a better way to share data besides props drilling or provide/inject.

jlrdw's avatar

@Sinnbeck just curious, why not a different layout customized for those pages?

Sinnbeck's avatar
Level 102

@jlrdw they do have different layouts. But each layout needs different data from laravel. In my case I have an admin panel that needs a menu structure based on permissions, and a chat which needs a user list and unread message counts. Both have different layouts but need completely different data from laravel.

@jd45 glad it could help. Yeah doing it yourself would probably be tricky and go against inertia. You can try checking out kinetic. It's view composers for inertia

1 like

Please or to participate in this conversation.