Skip to main content

Show User Data in Header

Premium
2 min read

In this short lesson, instead of hard-coded Hi, user, let's show the actual logged-in user.


To show the user, we will use the same concept of sharing data by adding it to the HandleInertiaRequests Middleware. Let's pass only the name and email.

app/Http/Middleware/HandleInertiaRequests.php:

class HandleInertiaRequests extends Middleware
{
// ...
 
public function share(Request $request): array
{
return array_merge(parent::share($request), [
'flash' => [
'message' => fn () => $request->session()->get('message')
],
'user' => [
'name' => $request->user()?->name,
'email' => $request->user()?->email,
],
]);
}
}

Now, we can access these values using...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…