怎么把laravel Elequent转换成收藏集?

时间:2019-12-04 19:38:34

标签: php laravel

我正在尝试获取laravel中记录的特定列并将其导出到excel,但是出现此错误。 我正在使用cyber-duck / laravel-excel程序包

  

类型错误:参数1传递给   Cyber​​duck \ LaravelExcel \ Exporter \ AbstractSpreadsheet :: load()必须是   Illuminate \ Support \ Collection的实例,给定的stdClass的实例,   呼入   /home/vagrant/code/sdf/app/Http/Controllers/Admin/ApplicantsController.php   在第51行

这是我的代码:

    public function export()
    {
        $applicants = Applicant::select('first_name','second_name','third_name','last_name',
            'qualification','birth_date','card_no','card_source','mobile','home','computer','english',
            'work','sex','course_id')->get();
        $fileName = 'applicants.xlsx';
        $excel = Exporter::make('Excel');
        $excel->load($applicants);
        return $excel->stream($fileName);
    }

1 个答案:

答案 0 :(得分:0)

这是我解决问题的方式:

public function export()
    {
        $applicants = DB::table('applicants')->select('first_name','second_name','third_name','last_name',
            'qualification','birth_date','card_no','card_source','mobile','home','computer','english',
            'work','sex','course_id')->get()->toArray();
            $app = collect($applicants);
        $fileName = 'applicants.xlsx';
        return $collection = Exporter::make('Excel')->load($app)->setSerialiser(new ApplicantSerializer)->stream($fileName);
    }