Onyx
Onyx

Reputation: 5772

Is there a better way of getting the profile image of the currently logged in user with Laravel?

Currently I'm trying to create an element that will contain the user's username and profile image. So far I've come up with a solution that works but I would like to know if there's any better options.

My solution:

<li>
    <img src='{{url("storage/uploads/profile_pictures/".Auth::user()-
    >profile_picture)}}'>
    <p>{{ Auth::user()->username }}</p>
</li>

Upvotes: 0

Views: 37

Answers (1)

DevK
DevK

Reputation: 9942

I'd suggest writing a function or an accessor in user model instead.

Something like this:

class User extends Authenticatable
{
    // ...

    public function profileImagePath()
    {
        return url("storage/uploads/profile_pictures/" . $this->profile_picture);
    }
}

And you'd use it like this:

<img src='{{auth()->user()->profileImagePath()}}'>

Upvotes: 1

Related Questions