Can’t load images from storage (Laravel)-> Failed to load resource: the server responded with a status of 404 (Not Found)

  dropzone, file-upload, image, laravel, local-storage

Hi everyone I have this problem where I am not able to display the images that I have in the storage folder.
I already connected the storage folder to the public one with the symlink (php artisan storage:link) but every time I try to visualize it I get this error from the console:
"Failed to load resource: the server responded with a status of 404 (Not Found)"

I don’t know if this could be useful but here’s my ProductController’s store function:

public function store(ProductRequest $request)
    {
        $user = Auth::user();
        $category = Category::find($request->category);
        $subcategory = Subcategory::find($request->subcategory);
        $product = new Product();
        $product->name = $request->name;
        $product->availability = $request->availability;
        $product->slug = Str::slug($request->name, '-', 'it').'-'.bin2hex(random_bytes(4));
        $product->description = $request->description;
        $product->price = $request->price;
        $product->category()->associate($category);
        $product->subcategory()->associate($subcategory);
        
        $product->save();
        $uniqueSecret = $request->uniqueSecret;
        $images = session()->get("images.{$uniqueSecret}", []);
        $removedImages = session()->get("removedImages.{$uniqueSecret}", []);
        $images = array_diff($images, $removedImages);

        foreach ($images as $image)
        {
            $newImage = new ProductImage();
            $fileName = basename($image);
            $newFileName = "/public/products/{$product->id}/{$fileName}";
            Storage::move($image, $newFileName);
            $newImage->file = $newFileName;
            $newImage->product_id = $product->id;
            $newImage->save();
        }
        File::deleteDirectory(storage_path("/app/public/temp/{$uniqueSecret}"));

        return redirect(route('product.create'))
    }

and here there is the code of the part of the view in which I recall the images:

@foreach ($product->images as $image)
                    
   <div class="carousel-item">
      <img class="d-block w-100" src="{{ asset($image['file'])}}" >
   </div>
 @endforeach

doing the dd($image['file'] i get:

/public/products/21/vZIxZfm6G8vDgmTv2zfW0aFSbotdnQ4UzWR59oXL.png

I tried also to add storage between public and products but it only creates a new folder inside storage/app named storage

(maybe could be useful: I’m using dropzone to upload the images directly from the website and apparently the upload process is working fine)

Don’t know what to do to solve this, hope someone could help me, thanks

Source: Laravel

Leave a Reply