charlesmun's avatar

Image not displaying

Am stuck and can't figure out why my images are not displaying.

Index function to send the services to the view

public function index(){

        $services = Service::all();

        foreach($services as $service) {
            $service->feature_image = Storage::url($service->feature_image);
        }

        return view('services.index')->with([
            'services' => $services,
        ]);
    }

Store function

    public function store(Request $request){

            $validate = Validator::make($request->all(), [
                'title' => 'required|string',
                'feature_image' => 'required|file|mimes:png,jpg,jpeg',
                'description' => 'required|string|min:30|max:150',
                'merchant_id' => 'required|string',
            ]);

            if( $validate->fails() ){
                return response($validate->errors(), 400);
            }

           //image storing before
           try {
            $image = $request->feature_image;
            $extension = $image->extension();
          
            if (!in_array($extension, ['jpg', 'jpeg', 'png'])) {
                throw new Exception('File format not supported');
            }
          
            $imageName = time().'_'.rand(1000, 9999).'.'.$extension;
          
            $imageName = $request->file('feature_image')->storeAs(
                'feature_images',
                $imageName,
                'local'
            );

            $service = new Service();
            $service->name = $request->title;
            $service->feature_image = $imageName;
            $service->short_description = $request->description;
            $service->merchant_id = $request->merchant_id;
            $service->save();
          
          } catch (Exception $e) {
            return response()->with('error', $e->getMessage());
          }
            
            $services = Service::where('merchant_id', '=', $request->merchant_id )->get();
            return response()->json([
                'services' => $services,
            ], 200);

    }

View thats not displaying the images

<td>
<img src="{{$service->feature_image}}" style="max-width:300px !important; max-height:100px !important; border-radius:none !important;" />
  </td>

Filesystems configuration

'local' => [
            'driver' => 'local',
            'root' => storage_path('app/private'),
            'visibility' => 'private',
        ],

and

'permissions' => [
        'file' => [
            'public' => 0644,
            'private' => 0600,
        ],
        'dir' => [
            'public' => 0755,
            'private' => 0700,
        ],
    ],

Images are uploading perfectly but just won't display

0 likes
12 replies
Shivamyadav's avatar

can you inspect your code and send that image url here to debug properly .. and send your images stored directory path here ?

Shivamyadav's avatar

@charlesmun your images should be under public dir to be accessable on the view page... create the link to the dir public/storage/feature_image.. to store your images in this folder and then you are able to access images in the view page (browser).

charlesmun's avatar

@Shivamyadav Thanks. Please clarify. In what instance would I store something in the storage folder? Suppose I am uploading PDF statements, I don't want them in the public folder, however, I want my users to be able to access them from their views...

Shivamyadav's avatar

@charlesmun you need to give the pdf download option by which a user can click on the pdf link and able to see it in their system , through the

Response::download(file path , 'filename.pdf', file type (pdf.png.jpg etc)
Shivamyadav's avatar

try this code for img tag

<td>
<img src="/app/private/{{$service->feature_image}}" style="max-width:300px !important; max-height:100px !important; border-radius:none !important;" />
  </td>
jlrdw's avatar

Try to foreach the images in view and try the asset helper.

charlesmun's avatar

I have changed the disk from the private one I intended to the public one and the image now shows

$imageName = $request->file('feature_image')->storeAs(
                'feature_images',
                $imageName,
                'public' //updated this line
);

Am thinking it's a permissions issue

Please or to participate in this conversation.