13

In my controller I have the code as below:

Excel::create('Laravel Excel', function($excel) {

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

            $sheet->setOrientation('landscape');

        });

    })->export('xls');

In config/app.php in aliases array i have defined this:

'Excel'     => 'Maatwebsite\Excel\ExcelServiceProvider',

I dont know why i cant make it work this library... Any idea?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Lulzim
  • 547
  • 4
  • 9
  • 22

4 Answers4

24

Instead of Excel::create you should use \Excel::create or add at the beginning of your file after current namespace use Excel; and then you will be able to use Excel::create

And the second error is that you used:

'Excel'     => 'Maatwebsite\Excel\ExcelServiceProvider',

and you should use:

'Excel' => 'Maatwebsite\Excel\Facades\Excel',

instead according to the docs.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Very strange that it still doesnt work :s Call to undefined method Maatwebsite\Excel\ExcelServiceProvider::create() – Lulzim May 10 '15 at 18:52
6

After all this you need to check whether or not you have this at the top:

use Maatwebsite\Excel\Facades\Excel;
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Mohammad Sheykholeslam
  • 1,379
  • 5
  • 18
  • 35
5

Sometimes, clearing configuration cache makes it work

php artisan config:cache

This should work after all you have followed all the instruction correctly but still getting "Class 'App\Http\Controllers\Excel' not found in Laravel" error

Tharindu Thisarasinghe
  • 3,846
  • 8
  • 39
  • 70
3

In laravel 5.4, First install the composer using following command

composer require maatwebsite/excel:~2.1

then, add the following in config/app.php file providers

Maatwebsite\Excel\ExcelServiceProvider::class,

then, add the following in config/app.php file aliases

'Excel' => Maatwebsite\Excel\Facades\Excel::class,

then, add this in your controller file

use Excel;
manikandan
  • 677
  • 2
  • 8
  • 19