Reputation:
I try to use Excel: @package maatwebsite/excel
package on laravel inside controller, but i get this error:
Non-static method Maatwebsite\Excel\Excel::create()
should not be called statically, assuming $this from incompatible context
my route is:
Route::get('all_users_report/{format}', 'SystemExportDataController@all_users_report');
my controller is:
class SystemExportDataController extends Controller
{
/**
* @param $format
*/
public function all_users_report($format)
{
Excel::create('all_users_records', function ($excel) use ($format) {
$excel->sheet('My Site - all users report', function ($sheet) use ($format) {
........
});
})->export($format);
}
}
POST UPDATED 2
'providers' => [
...
'Maatwebsite\Excel\ExcelServiceProvider'
],
'aliases' => [
...
'Excel' => 'Maatwebsite\Excel\Facades\Excel'
],
Error:
BadMethodCallException in ServiceProvider.php line 234:
Call to undefined method [package]
UPDATE 3:
Full Class Controller:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Maatwebsite\Excel\Excel;
class SystemExportDataController extends Controller
{
public function all_users_report($format)
{
Excel::create('all_users_records', function ($excel) use ($format) {
$excel->sheet('Epay.Li - all users report', function ($sheet) use ($format) {
$all_records = ChangeMoney::all();
$sheet->loadView('layouts.admin.all_users_report',
array(
'all_records' => $all_records,
)
);
});
})->export($format);
}
}
Upvotes: 2
Views: 12176
Reputation: 143
Use use Maatwebsite\Excel\Facades\Excel
not use Maatwebsite\Excel\Excel;
when you are calling static method like Excel::create()
Upvotes: 8
Reputation: 1556
I have found I need to prepend a backslash (signifies the root namespace) to make sure it's using the Facade. This would make your call as follows:
\Excel::create('all_users_records', function ($excel) use ($format) {
$excel->sheet('Epay.Li - all users report', function ($sheet) use ($format) {
$all_records = ChangeMoney::all();
$sheet->loadView('layouts.admin.all_users_report',
array(
'all_records' => $all_records,
)
);
});
})->export($format);
Upvotes: 1
Reputation:
You should add 'Excel' => 'Maatwebsite\Excel\Facades\Excel'
to your facades array in config/app.php
and 'Maatwebsite\Excel\Excel\ServiceProvider'
to your providers in order to use it the way you do.
Or you can make an instance of the class using $excel = App::make('excel');
and then $excel->create(...
.
UPDATE
There are two ways to call the Excel package:
One of them is to use the facade, but you have to add 'Excel' => 'Maatwebsite\Excel\Facades\Excel'
in your facades array in config/app.php
and then
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
class SystemExportDataController extends Controller
{
public function all_users_report($format)
{
Excel::create('all_users_records', function ($excel) use ($format) {
$excel->sheet('Epay.Li - all users report', function ($sheet) use ($format) {
$all_records = ChangeMoney::all();
$sheet->loadView('layouts.admin.all_users_report',
array(
'all_records' => $all_records,
)
);
});
})->export($format);
}
}
Note that I removed the second use
. Also if you don't want to add the facade in the facades array, you could use it - use Maatwebsite\Excel\Facades\Excel
.
The other way is to make an instance of the package and use that instance, like this:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
class SystemExportDataController extends Controller
{
public function all_users_report($format)
{
$excel = App::make('excel');
$excel->create('all_users_records', function ($excel) use ($format) {
$excel->sheet('Epay.Li - all users report', function ($sheet) use ($format) {
$all_records = ChangeMoney::all();
$sheet->loadView('layouts.admin.all_users_report',
array(
'all_records' => $all_records,
)
);
});
})->export($format);
}
}
Upvotes: 2