0

I am using Laravel plugin Laravel Excel to load view directly into excel sheet. Everything goes fine. But while using image in my view, error prompts saying the full url of the image not found. Error is:

PHPExcel_Exception

File http://example.com/client1_site/public/uploads/patients/23/first-patient-photo.jpg not found!

But the url exists and it displays the proper image.

I have following html in my view:

<td>
    @if(!is_null($s->photo))
        <img src="{{URL::to($s->photo)}}" style="width:90px;height:85px;" alt=""/>
    @endif
</td>

Anyone having embedded image in excel using the laravel-excel plugin experienced the same?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Sangam Uprety
  • 1,482
  • 2
  • 20
  • 38

5 Answers5

3

I've tested it and it seems you only can use relative paths when you want to attach file to Laravel Excel.

So you cannot use for example:

but you should use:

<img src="img/ph4.png" style="width:90px;height:85px;" alt=""/>

I've prepared test code to make it work both for Laravel Excel and for normal route if necessary:

routes.php file

Route::get('/', function() {

    Excel::create('New file', function($excel) {

        $excel->sheet('New sheet', function($sheet) {

            $sheet->loadView('hello')->with('no_asset', true);

        });

    })->download('xls');
});


Route::get('/test', function() {

    return View::make('hello');
});

hello.blade.php file:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<table>
<tr>
<td>
info here
</td>


<td>

@if (isset($no_asset))
    <img src="img/ph4.png" style="width:90px;height:85px;" alt=""/>
@else
    <img src="{{ asset('img/ph4.png') }}" style="width:90px;height:85px;" alt=""/>
@endif
</td>
</tr>
</table>

</body>
</html>

Passing extra variable no_asset to view you may decide inside view if you display only relative path or whole path ussing asset.

It's working for me - both using /test route I get expected result and for / I recieve excel file with image.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
1

Use pulic_path() it returns the fully qualified path to the resources directory.

<td>
    @if(!is_null($s->photo))
        <img src="{{ public_path().$s->photo }}" style="width:90px;height:85px;" alt=""/>
    @endif
</td>
Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
1

I also faced this issue and solved it with public_path(). In my case

<img src="{{ public_path().'/image/sample.png' }}">
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
ncs ncs
  • 51
  • 2
0

You have used following URL:

File http://example.com/client1_site/public/uploads/patients/23/first-patient-photo.jpg

But if this is in your public folder then the URL should be:

File http://example.com/uploads/patients/23/first-patient-photo.jpg

Use asset helper method instead:

<img src="{{ asset('uploads/patients/23/first-patient-photo.jpg')}}" style="width:90px;height:85px;" alt=""/>

Use the path after public, adjust it in your code according to this rule.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • 1
    http://example.com in my case is http://localhost. Since stackoverflow won't allow localhost, I instead chose example.com. So apache's folder client_site will be actually a part of the URL before public. Even with the asset() way, same image url is generated. And same error. – Sangam Uprety Sep 24 '14 at 09:43
  • Same issue here - did you manage to solve it please? – Luke Galea May 02 '22 at 12:21
0

You can use <img src="public/uploads/patients/23/first-patient-photo.jpg" />