Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

elo's avatar
Level 3

Return full image url in API resource

I am working on a Laravel 5.8 API application and I want to pass an image attribute with the value as the full url of the image. So I have created a symbolic link from public/storage to storage/app/public by running the artisan command

php artisan storage:link

I am storing the image in my controller like this

if ($product = Product::create([
        'name'      => $request->name,
        'category'  => $request->category,
        'status'    => $request->status,
        'price'     => $request->price,
        'interest'  => $request->interest,
    ])) {
        // store the product image
        $file = $request->file('image');
        $destinationPath = "public/images/products";
        $filename = $product->name . '_' . $product->id . '.' . $file->extension();

        Storage::putFileAs($destinationPath, $file, $filename);

        ProductImage::create([
            'product_id'    => $product->id,
            'name'          => $filename
        ]);
    }

This is what the ProductResource looks like

return [
    'id'        => $this->id,
    'name'      => $this->name,
    'category'  => $this->category,
    'status'    => $this->status,
    'price'     => $this->price,
    'interest'  => $this->interest,
    'image'     => 'http://localhost:8000/api/v1/public/images/products/' . $this->image->name,
];

Reason why I want to return the full image path is so that the React application consuming the api can simply pass the image path inside the <img src="">

Is this proper way to do it?

0 likes
6 replies
aurawindsurfing's avatar

Hi @elo

I suppose there is nothing wrong with it http://localhost:8000/api/v1/public/images/products/

Until you realise that you want to move to a production server and all your images are locked to your local machine. What you should do is to keep the id part of the path in database and let your code build up the correct url to the resource. You might later store your images on let's say S3 or Cloudinary and then this will become a problem.

Hope it helps!

elo's avatar
Level 3

Moving to say a staging or production environment is the reason I am asking this question. For instance I have a staging environment staging.domain.com where I am pushing my code to so the team can have access to the API. How can I write my code so it builds the correct url? What did you mean by "keeping the id part of the path in database"?

aurawindsurfing's avatar

@elo I mean that in your code / logic you build up your url but you keep unique part let's say its ID in the database table called images

elo's avatar
Level 3

Right, the productImages table has an id, product_id & name fields

Snapey's avatar
Snapey
Best Answer
Level 122

The main issue is that you have public in your URLs

This is a NO NO NO NO NEVER

Do not host your site where public is visible in your URL since then other sensitive files are also visible.

You should first fix your local hosting so that Document Root is the public folder, and that your development environment more closely resembles production.

You should use the Laravel helpers to create a path to the site and not hard code localhost and port into your code.

eg

return [
    'id'        => $this->id,
    'name'      => $this->name,
    'category'  => $this->category,
    'status'    => $this->status,
    'price'     => $this->price,
    'interest'  => $this->interest,
    'image'     => asset('images/products/' . $this->image->name),
];


2 likes
elo's avatar
Level 3

Thanks for pointing that issue of having the public in the url out.

1 like

Please or to participate in this conversation.