Php 如何将laravel Elequent转换为collection?
我试图在laravel中获取特定的记录列并将它们导出到excel,但我遇到了这个错误。 我正在使用cyber duck/laravel excel软件包 类型错误:参数1传递给 Cyberduck\LaravelExcel\Exporter\AbstractSpreadsheet::load()必须是 Lightlight\Support\Collection实例,给定的stdClass实例, 叫来 /home/vagrant/code/sdf/app/Http/Controllers/Admin/applicationscocontroller.php 在线51 这是我的代码:Php 如何将laravel Elequent转换为collection?,php,laravel,Php,Laravel,我试图在laravel中获取特定的记录列并将它们导出到excel,但我遇到了这个错误。 我正在使用cyber duck/laravel excel软件包 类型错误:参数1传递给 Cyberduck\LaravelExcel\Exporter\AbstractSpreadsheet::load()必须是 Lightlight\Support\Collection实例,给定的stdClass实例, 叫来 /home/vagrant/code/sdf/app/Http/Controllers/Admi
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);
}
这就是我解决问题的方法:
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);
}
这就是我解决问题的方法:
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);
}
为什么不改用
->loadQuery
并从$applicators
中删除->get()
?我遇到了这个错误:调用undefined方法illumb\Database\Query\Builder::loadQuery(),顺便说一句,我正在使用laravel 5.4,请尝试一下<代码>$excel->load(收集($applicators->toArray())@user2094178 select/get()本身应该返回一个集合,我不知道它返回stdClass时会出现什么情况?为什么不改用->loadQuery
,并从$applicators
中删除->get()
?我得到这个错误:调用未定义的方法illumb\Database\Query\Builder::loadQuery()Btw,我正在使用laravel 5.4尝试一下<代码>$excel->load(收集($applicators->toArray())@user2094178 select/get()本身应该返回一个集合,我不知道它返回stdClass时会出现什么情况?