Home > Mobile >  Laravel link with username on several pages
Laravel link with username on several pages

Time:03-27

I've created a small blade file that I will use on several pages of my application. It's special navigation for the users who are logged in. One of the links should redirect users to their profile. I'm using a username for it like this: "profiles/{{username}}". How can I pass the username to the blade that will be used on several pages? Some of those pages already make use of user data but some of them do not need it. Personally, I don't see the point to pass the whole user data to all pages when I only need a username. How can I achieve it?

CodePudding user response:

use Illuminate\Support\Facades\Auth;

// Retrieve the currently authenticated user...

$user = Auth::user();

// Retrieve the currently authenticated user's ID...

$id = Auth::id();

// can also check if the user is logged in

if (Auth::check()) {
    Auth::user()->username
}

//on the view , you can have something like this:

@if (Auth::check())
  Auth::user()->username
@endif

This can also be done on your view template with the @ a-notation

on the view page , always add {{}} to print your data. for instance

//For printing username on the view
{{Auth::user()->username }}

//For printing id on the view
{{Auth::user()->id }}
  • Related