Fahad Khan
Fahad Khan

Reputation: 375

Unable to retrieve images in View in Laravel 5.3

I have uploaded images in storage folder. Now I want to retrieve the images in the view. I am working local environment right now. <img src={{ Storage::disk('local')->url($image->path) }}> doesn't to work. What am I missing?

Upvotes: 0

Views: 762

Answers (2)

S&#233;rgio Reis
S&#233;rgio Reis

Reputation: 2523

Why not make a function that receives either a name or a an ID of an model or whatever you have and return the image according to some logic? For example :

  1. Get the filename(if you save it under a folder make sure to save folder/filename.extension, if)
  2. Search for it and get the file in the correct storage
  3. Return the file with the correct headers
  4. Make a GET route that recieves as a parameter the filename and call a funtion has shown below
 // Controller
public function getImage(Request $request){
   $filename = $request->filename;
   $file = Storage::disk('local')->get($filename);
   return response($file)->withHeaders(['Content-Type' => "image/png"]);
}


 // Route
Route::get('/getThisImage/{filename}',[
 'uses'=>'HomeController@getImage',
 'as'=>'getImage'
]);

// Example of calling it in blade
<img src="{{route('getImage')}}/{{$image->path}}"> 

Upvotes: 0

Parth Vora
Parth Vora

Reputation: 4114

First, make sure that you have setup symlink properly between public and storage/app/public directories. You can do this by using this command:

php artisan storage:link

For more info please have a look: https://laravel.com/docs/5.3/filesystem#configuration

In view, you can display an image like this:

<img src="{{ asset($image->path) }}" />

Also, make sure that you storing image path correctly in the database.

Upvotes: 2

Related Questions