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

Innominate's avatar

Auth username intials in blade template

In blade template I use {{ Auth::user()->name }} to diplay the username of the logged in user, I'd like to be able to display the initials and instead of putting the php code directly in the template which isn't very elegant I'd like to have something like {{ Auth::user()->initials }}

What's best practice for doing such? Any examples? Thanks

0 likes
10 replies
AlexYa's avatar

As a case you can create a method in User Model and call it in blade template

public function initials()
{
    $initials = ***your code to create this value***
    return $initials;
}
1 like
Innominate's avatar

@AlexYa Looks like this is what I need. Could you possibly elaborate on your example? I'm not quite sure how to implement this. Refering me to the proper documentation works as well, I can't seem to find what I need. Thanks!

ohffs's avatar

You can get the first letter of a string by just doing $string[0] - so for example you could just return $this->firstname[0] . $this->secondname[0] (or format it however you need).

1 like
Innominate's avatar

@ohffs Thanks for your reply! I'm familiar with that part already :-) It's creating the method that confuses me a bit.

pmall's avatar

It's creating the method that confuses me a bit.

What confuses you ? You have a model class, put a method in it.

Innominate's avatar

Everything I needed was actually here. Being sick + tired isn't the best combination, had me confused. Got it working, thanks and sorry for my confusion :-)

maelga's avatar

As @AlexYa said, you can create an initials() function in your user model:

public function initials(){
    $words = explode(" ", $this->name );
    $initials = null;
    foreach ($words as $w) {
        $initials .= $w[0];
    }
    return strtoupper($initials);
 }

Then simply call your initials() function in your blade view: {{ Auth::user()->initials() }} instead of {{ Auth::user()->name }}

6 likes
Snapey's avatar

@maelga please take note of the date of posts. This post was solved over a year ago.

MPGsKrieg's avatar

With the new Laravel starter kits (livewire) the initials public function is used but I have firstname and lastname for users - so I needed to amend the initials script. Think I have read the above correctly but I would appreciate someone telling me if this is; a) okay b) a sensible solution c) if there is a better way given its 2025 and a lot has changed since this post was solved, because I have 2 variables specifically I think I can remove the explode - implode methods

Function in starter kits for reference

public function initials(): string { return Str::of($this->name) ->explode(' ') ->map(fn (string $name) => Str::of($name)->substr(0, 1)) ->implode('');

My code

public function initials(): string {

    $firstname = 'firstname';
    $lastname = 'lastname';

    $initials = Str::of($this->$firstname[0] . $this->$lastname[0]);
       return strtoupper ($initials);
}

Please or to participate in this conversation.