marybane marybane - 9 months ago 68
PHP Question

Can't get images from storage laravel 5

Hey so i'm trying to put my images into a slideshow i have on my index.
the images are saved on

, and then the rest of the path i retrive from my database.

I managed to output the correct path on the
<img src>
tag, and the title gets output like its supposed to, but the image won't.

I send and array from the controller and then cycle through it on my view:


public function index()
$projects = Project::all()->take(4);
return view('index' , compact('projects'));


Index view

@foreach($projects as $project)
{{$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix()}}
<li><img src="{{$storagePath.$project->fetchMedia->first()->public_name}}" title=" {{$project->name}}"></li>

The HTML output is the correct path : "/home/vagrant/project/storage/app/projects/Domaa6.jpg"

but it doesn't show the image.

I already tried to use the HTML::image helper but had no success either.

is there a way to get it working like it is or do i need to create a new controller to manipulate my images?

is the function on my model that returns my Media.

public function fetchMedia()
return $this->hasMany('App\Media');

Answer Source

What you are trying to do is not possible in the storage directory but possible only in public directory, also exposing the path or URL to your laravel storage directory creates some vulnerability and its bad practice

However there is a way to workaround it:

First, you need an Image package to generate the image dynamically when a certain route is referenced, I recommend intervention/image which is has many methods that makes image manage a breeze.

To achieve this take the following steps:

1. Install Intervention Image:

Add Intervention Image to yourr composer.json and the do composer update

"require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "intervention/image": "2.*",
        "intervention/imagecache": "2.*"


In the $providers array add the service providers for this package.


Add the facade of this package to the $aliases array.

'Image' => 'Intervention\Image\Facades\Image'

Configure Intervention Image in Laravel 5

$ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"

Publish configuration in Laravel 4

$ php artisan config:publish intervention/image

For more installation detail

After you install intervention/image you can do something like this:


2. Setup a route that will handle image requests

Route::get('stock-photos/{image}', function($image){

    //do so other checks here if you wish

    if(!File::exists( $image=storage_path("stock-photos/{$image}") )) abort(404);

    return Image::make($image)->response('jpg'); //will ensure a jpg is always returned

3. Then later in the view you can do:

@foreach($projects as $project)
        <img src="{{url('stock-photos/'. $project->fetchMedia->first()->public_name)}}" title="{{$project->name}}">