octoxan's avatar

Trying to use Storage and running into lots of problems.

So I've ssh'd into Homestead and have run art storage:link. I can see the symlink has been created just fine.

Now, in one of my controllers I'm saving an uploaded file to storage like so...

$profile_photo = $request->file('profile_photo')->store('profile-photos');

This is saving the image in storage/app/profile-photos. Now, how do I go about viewing this image on the frontend, without using Blade?

Am I supposed to manually create a symlink every time I use a custom directory? I'd thought storing it in profile-photos would put it in storage/app/public/profile-photos so it could show up on the frontend.

If I were to do this however...

$profile_photo = $request->file('profile_photo')->store('public/profile-photos');

I can view it at example.com/storage/profile-photos/filename.png but since I'm saving the file path in the user table this saves it as "public/profile-photos/filename.png"

Am I missing something? Surely I wouldn't be doing a str_replace for public with storage before saving it in the database, right?

I'm just looking for the best way to let users upload photos, store them in storage, and then call them on the frontend (I'm using Vue entirely on the frontend). So I'd like to just be able to access these at URLs.

0 likes
3 replies
Snapey's avatar

Because of the symlink, you are storing at a different path to the URL you need.

You could store the file in the public/storage folder instead and then the path would be similar.

Usually though I would just store the common part of the URL in the database, eg profile-photos/filename and then adapt that at runtime for path or url as required

octoxan's avatar

@Snapey so you'd do a str_replace to remove public from the url ->store() returns to store only the common part of the url in the database?

When I do ->store() with some directory as the argument, the directory is in the returned data, which seems silly since I'd have to do a str_replace to remove 'public' every time I use this function?

Snapey's avatar

No I don't mess with the strings. Just prefix the string with the appropriate one for the use at that moment

Please or to participate in this conversation.