TheDeveloper's avatar

How to access locally saved images front front end

Hey guys, so I have Vue front end and Laravel as a backend api, they're separate code bases.

I've managed to get image uploads working, but how can I access these on my front end which is a separate code base? I've seen something about storage link but not sure if that works for when the code bases are split?

Thanks in advanced.

0 likes
10 replies
Snapey's avatar

In Your API responses, you need to return complete URIs for the images, and store them in storage/app/public (with a symlink to public/storage) or store them directly in the public folder.

tykus's avatar

Are the images publicly accessible on the server-side application; if so then all you need is to pass the full URL to the frontend application?

EDIT @snapey go there first 👍

TheDeveloper's avatar

So what would the url look like for an img tag?

Thanks for replies.

Snapey's avatar

@TheDeveloper

So what would the url look like for an img tag

You don't know what a URI looks like?

https://yourdomain.com/foldername/imagename.ext
TheDeveloper's avatar

This is how the file_name (the image) is saved and comes back on the front end

file_path: "/private/var/folders/sf/16gz_7bs47s_kc24wx5437f00000gn/T/phpgvLoYS"

TheDeveloper's avatar

@tykus Yeah I am missing something. I can view the images by going /storage/my-folder/image name

But I think I am storing them wrong because as above is what the image file_path comes out as. I need to the actual file name then I am guessing?

TheDeveloper's avatar

@tykus I actually have managed it but with some bodgery.

$path = $request->file('file_path')->store('public/league-pictures'); $league = new League; $league->name = $request->name; $league->description = $request->description; $league->file_path = $path; $league->save();

That's how I'm storing it, they have the file_path of public/league-pictures, I have to append it on the frontend, remove the public and swap it for storage and they load!

tykus's avatar

@TheDeveloper you can use the url helper in an accessor method to generate the image URL; and furthermore, you automatically append it to the JSON representation of the Model.

Please or to participate in this conversation.