codemode's avatar

Getting the right image path to display in the view

Hello,

I'm saving the uploaded image in the controller as :

$profile_pic = request()->file('profilepic')->store('public/avatars');

.. so the image goes into storage/app/public/avatars - storing correctly. BUT, it stores the image's name with prefixing "public/avatars" before it (Question: is it possible to avoid this?)

Now, in displaying the picture, i'm extracting the image name as

$current_profile_pic = '/storage/' . Auth::user()->profile_pic;

And finally, displaying in the view as

<img src="{{ url($current_profile_pic) }}">

Here in the view, the URL is "http://app.test/storage/public/avatars/jkAYAaLIjmPR0tXc90Q1DcRmuPSCHBHk3ArWqQu9.png"

Instead of : "http://app.test/storage/avatars/jkAYAaLIjmPR0tXc90Q1DcRmuPSCHBHk3ArWqQu9.png" (which works)

I have executed : php artisan storage:link

Is there a way to store the image without storing the "public" prefix in my DB?

0 likes
1 reply
munazzil's avatar

Check with below,

      <img src="{{ asset($current_profile_pic) }}">

or

     <img src="{{ asset('public/avatars'.$current_profile_pic) }}">

Please or to participate in this conversation.