gpt4 book ai didi

Laravel 在 Controller 内部使用 Excel 包

转载 作者:行者123 更新时间:2023-12-05 09:21:07 25 4
gpt4 key购买 nike

我尝试在 Controller 内部的 laravel 上使用 Excel: @package maatwebsite/excel 包,但我收到此错误:

Non-static method Maatwebsite\Excel\Excel::create() 
should not be called statically, assuming $this from incompatible context

我的路线是:

Route::get('all_users_report/{format}', 'SystemExportDataController@all_users_report');

我的 Controller 是:

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);
}
}

发布更新 2

'providers' => [
...
'Maatwebsite\Excel\ExcelServiceProvider'
],
'aliases' => [
...
'Excel' => 'Maatwebsite\Excel\Facades\Excel'
],

错误:

BadMethodCallException in ServiceProvider.php line 234: 
Call to undefined method [package]

更新 3:

全类 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);
}
}

最佳答案

使用 use Maatwebsite\Excel\Facades\Excel 而不是 use Maatwebsite\Excel\Excel; 当你调用像 Excel::create() 这样的静态方法时

关于Laravel 在 Controller 内部使用 Excel 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34230959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com