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

yansusanto's avatar

How to prepend character before username automatically?

Hi everyone,

I'd like to know if there is a way to prepend, say @ automatically or do we have to do it manually <a href="@{{ $user->username }}</a>?

What is the usual practice?

Thanks!

0 likes
5 replies
yansusanto's avatar

Thanks, @Cronix

Is this correct as I'm getting Undefined property: App\User::$username?

    public function getUsernameAttribute()
    {
        return '@' . $this->username;
    }
Cronix's avatar

I wouldn't do that to username (getUsernameAttribute()). Then everytime you call $user->username it will add the @. I'm sure you want it added sometimes, but not all of the time. That's why I put it in mentionName, so you could still use username.

Is username an actual field in the database for a user? I only used it because you used it in your original post.

yansusanto's avatar

Yes, username is an actual field.

I wouldn't do that to username (getUsernameAttribute()). Then everytime you call $user->username it will add the @.

You're right. I'll use another name.

hanif-king's avatar

@yansusanto when your problem solved mark it as solved question because it will help others to not look toward unsolved questions. thanks :)

Please or to participate in this conversation.