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

Anaxarchos's avatar

Laravel 11 and passing authenticated user data to Inertia props

Hi, in Laravel 10 I could pass data from the authenticated user via the HandleInertiaRequests class. When I do this in Laravel 11 I get an empty object in my vue components. Now I have to use the Inertia::share function in the AppServiceProvider to pass this data. Is this the right way, or am I missing something?

0 likes
2 replies
LaryAI's avatar
Level 58

In Laravel 11, you can still pass data to Inertia components using the HandleInertiaRequests middleware, but there might be some changes in how you should implement it compared to Laravel 10. If you're getting an empty object, it's possible that there's an issue with how the data is being shared or with the session/authentication state at the time of the request.

However, using the Inertia::share method within a service provider is indeed a correct and common way to share data with all Inertia responses. This is especially useful for data that needs to be available globally, like authenticated user data.

Here's how you can share the authenticated user data using the Inertia::share method in your AppServiceProvider:

use Inertia\Inertia;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Share data with all Inertia responses
        Inertia::share([
            // Always share the authenticated user
            'auth.user' => function () {
                return Auth::user() ? Auth::user()->only('id', 'name', 'email') : null;
            },
            // You can also share flash messages or errors
            'flash' => function () {
                return [
                    'success' => session('success'),
                    'error' => session('error'),
                ];
            },
        ]);
    }
}

In this example, we're using a closure to lazily load the authenticated user data, which ensures that it's only included when needed and after the user has been authenticated. We're also using the only method to restrict the user data to specific fields for security and privacy reasons.

Remember to import the necessary classes at the top of your AppServiceProvider file.

After setting this up, you should be able to access the auth.user object in your Vue components like this:

export default {
  mounted() {
    console.log(this.$page.props.auth.user);
  }
}

If you're still facing issues, make sure that your user is actually authenticated at the time of the request and that the session is correctly configured. Also, check for any changes in the Inertia.js or Laravel upgrade guides that might affect how data is shared or sessions are handled.

1 like
Anaxarchos's avatar

@LaryAI This is exactly the way, I pass my user data to my vue components. Thanx :-)

1 like

Please or to participate in this conversation.