Ssionn's avatar

Image uploading using storage:link

I have an issue of which I can't quite understand the concept of storage:link and viewing images. Now I know it makes a url for it to be shown, but that URL doesn't exist for me, even though it gets pushed to the front end.

This is what i'm talking about.

<img src="{{ Auth::user()->getImageUrl() }}" alt="Profile Image" class="w-64 h-64 bg-white rounded-xl">

Okay so i'm calling the getImageUrl method from the authenticated user. Now let's see that function.

public function getImageUrl(): string
{
    if ($this->image) {
        return asset('storage/' . $this->image);
    }

    return 'https://ui-avatars.com/api/?name=' . urlencode($this->name);
}

So it retrieves the the image of the authenticated user and parses it as an url, otherwise it just generates the random initials of the account name.

In my database the image is stored as images/{filename.extension}.

if ($request->hasFile('image')) 
{
    $path = $request->file('image')->store('images');

    $user->update([
        'image' => $path,
    ]);

    return redirect()->route('profile', $user->username);
}

My question is why isn't the url generation working or not recognising. is there something i'm doing wrong?

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

have you customised config/filesystem.php at all ?

have you sucessfully run php artisan storage:link ?

when you look in the public/storage folder do you see the exact same files and folders as storage/app/public? ( you should)

1 like
Ssionn's avatar

@Snapey

  1. No I don't have to.

  2. Yes.

  3. Yes.

But you made me realise that i needed to add ['disk' => 'public'] as an extra parameter to the store method because of the config question. Thanks :P

Please or to participate in this conversation.