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

elo's avatar
Level 3

Uploading and Displaying Images Stored in Storage

Hello awesome devs. I am struggling trying to upload image files to local storage and display the same images on Nova. I have integrated Nova into a laravel API project to allow admin perform certain task like uploading a product which includes uploading product images. Currently this is a result of creating a product using postman

{
    "responseMessage": "Successful operation",
    "responseStatus": 200,
    "product": {
        "id": 17,
        "name": "AGO-0003",
        "category": "AGO",
        "description": "AGO is purpolarly called Diesel.\n\nDiesel fuel, also called diesel oil, combustible liquid used as fuel for diesel engines, ordinarily obtained from fractions of crude oil that are less volatile than the fractions used in gasoline.",
        "status": "Loading",
        "price": 100000,
        "units": "528",
        "measurement": "Units",
        "return": "46",
        "cycle": "10",
        "returnRangefrom": "30",
        "returnRangeto": "50",
        "start": "2019-10-18T23:00:00.000000Z",
        "hidden": null,
        "hoursTillStart": 72,
        "imageUrl": "http://localhost:8000/storage/images/products/pramopro_AGO-0003_17.jpeg"
    }
}

This is the store method from the controller responsible for storing new product resources

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

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

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

The Product model has one ProductImage model, that is HasOne relationship. In my Product nova resource, I am trying to display the images for each product like this

public function fields(Request $request)
{
    return [
        Image::make('Image', function () {
            return 'images/products/' . $this->image->name;
        }),

        Text::make('Name')
            ->sortable()->rules('required'),

        // more fields here but trimmed 

        File::make('Product Image')
            ->disk('public')
            ->path('images\products')
            ->storeAs(function (Request $request) {
                'pramopro_' . str_replace(' ', '', $this->name) . '_' . $this->id . '.' . 'jpg';
            }),
    ];
}

In nova, the image is not displayed and I noticed that the url is localhost/storage/images/products/pramopro_AGO-0003_17.jpeg. This is the case on my development and staging environments.

When I try to upload an image when creating a new product, I get the error fopen(C:\home directory path\AppName\storage\app/public\images/products): failed to open stream: Permission denied

I am lost at the moment. Help

0 likes
0 replies

Please or to participate in this conversation.