Laravel - 调用未定义的方法Maatwebsite \ Excel \ Excel :: create()

时间:2018-03-21 08:13:21

标签: laravel laravel-5

即使我在config / app.php中添加了服务提供程序和别名,我仍然收到此错误

Controller.php这样

    public function export_app_csv(){
        $data = Item::get()->toArray();
        return Excel::create('test_export', function($excel) use ($data) {
                $excel->sheet('mySheet', function($sheet) use ($data)
                {
                    $sheet->fromArray($data);
                });
         })->download('xlsx');
    }

配置/ app.php

    /*
     * Package Service Providers...
     */
    Maatwebsite\Excel\ExcelServiceProvider::class,

    /* aliases */
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,

控制器中的方法只是一个站点的示例。因为我还不知道laravel excel是如何运作的。

然后docs给了我更多问题。

namespace App\Exports;

class InvoicesExport implements FromCollection
{
    public function collection()
    {
        return Invoice::all();
    }
}

上面的代码给了我Interface 'App\FromCollection' not found

1 个答案:

答案 0 :(得分:0)

如果您正确遵循文档,这将有效。

public function get_export_csv(){
    $allData = $this->data->get();

    $module = $this->module;

    Excel::create('projects', function($excel) use ($allData, $module) {

        $excel->sheet('sheet1', function($sheet) use ($allData, $module) {
            $sheet->loadView('admin.'.$this->module.'.excel', compact('allData','module'));
        });
    })->download('csv');

}

在您的刀片中,您只需要此表

<table class="table table-bordered table-hover">
    <thead>
    <tr>
        <th>#</th>
        <th>First Name</th>

    </tr>
    </thead>
    <tbody>
    <?php $count = 0; ?>
    @foreach($allData as $row)
        <?php $count++; ?>
        <tr>
            <td>{{$count}}</td>
            <td>{{$row->first_name}}</td>

        </tr>
    @endforeach
     </tbody>
</table>