Friedrich's avatar

After uploading image in laravel get the image url

How to after uploading image in laravel get the image url ?

0 likes
16 replies
Sergiu17's avatar
return url('images', $image->name); 

// http://localhost/images/image-name.png
2 likes
Friedrich's avatar

@Sergiu17 Thanks but does not work if i use it on shopify Can you suggest some steps that make work just like using third party sites that after I upload image on laravel inputs then i got image url then accessible online thanks

Friedrich's avatar

@martinbean What i want is after uploaded it on laravel storage how can i get the image url that can access online?

martinbean's avatar

@Friedrich And what I want is for you to actually show how you’re currently uploading files, so I can then advise you on the best way to get the URL for that file.

1 like
Friedrich's avatar

@martinbean

 $product = new ImageProducts;
            $product->user_id = $userId;
            $product->product_id = $prod;

            for ($i = 1; $i <= 5; $i++) {
                if ($request->hasFile('image' . $i)) {
                    $file = $request->file('image' . $i);
                    $path = $file->store('public'); // Store the file in the 'images' directory

                    // Save the file path to the corresponding column
                    $product->{'image' . $i} = $path;
                }
            }

            $product->save();

After i upload it on laravel storage i want to get a source url online to use it

martinbean's avatar

@Friedrich You get the URL using the aptly-named url method on the Storage facade:

$url = Storage::disk('public')->url($path);
Snapey's avatar

@Friedrich when your request is received by example.com then it will be that in the url. Dont save the url in the database as it limits your ability to publish the site under any domain

christogonus's avatar

If the generated url works file when you visit with the directly generated URL, it means the problem of not showing in shopify is caused by disabled features on your server.

Be sure your host did not disable hotlinking.

Friedrich's avatar

<img src="public/3fesIcD8TxXwq8SyeZuZXkOuWbdck8fxyqQL6sPU.png" alt="test">

Guys @sergiu17 @martinbean @christogonus

this is my code at shopify i want to change it to the online source image to able to acces it on shopify page take note i inserting this html at page content of shopify

Snapey's avatar

@Friedrich Well hard to say, because this is clearly wrong

<img src="public/3fesIcD8TxXwq8SyeZuZXkOuWbdck8fxyqQL6sPU.png" alt="test">

public should never appear in any URLs or asset paths.

if the file was in storage/images/3fesIcD8TxXwq8SyeZuZXkOuWbdck8fxyqQL6sPU.png then it could be like

img src="{{ config('app.url') }}/{{$image->url }}">

etc, depending on what you have in the database

christogonus's avatar

@Friedrich I think the problem is that you are confusing the problem of HTML with laravel.

Could you please confirm that this laravel installation is online?

If you do for instance have the laravel app on https://friedrich.shop, then after uploading the product images, try to view the images directly on the browser from https://friedrich.shop/public/3fesIcD8TxXwq8SyeZuZXkOuWbdck8fxyqQL6sPU.png

If the link correctly opens the image, good, then use the html for image should look like

<img src="https://friedrich.shop/public/3fesIcD8TxXwq8SyeZuZXkOuWbdck8fxyqQL6sPU.png" alt="test">

If this doesn't work, then

  1. ensure that you ran the "php artisan storage:link"
  2. share the link to your working website (where you have these product images uploaded and working)

Please or to participate in this conversation.