Laravel 导出到excel时出现非法偏移类型错误
我正在尝试导出excel,但当我导出它时,它会给我一个非法偏移类型错误的错误。我怎样才能让它工作? 这是我的UsersController中的代码Laravel 导出到excel时出现非法偏移类型错误,laravel,laravel-5,laravel-5.5,maatwebsite-excel,Laravel,Laravel 5,Laravel 5.5,Maatwebsite Excel,我正在尝试导出excel,但当我导出它时,它会给我一个非法偏移类型错误的错误。我怎样才能让它工作? 这是我的UsersController中的代码 public function userExport() { $users = $this->users->paginate( $perPage = 20, Input::get('search'), Input::get('status'),
public function userExport()
{
$users = $this->users->paginate(
$perPage = 20,
Input::get('search'),
Input::get('status'),
Input::get('emp_status'),
Input::get('level'),
Input::get('age'),
Input::get('gender'),
Input::get('civil_status'),
Input::get('role_id'),
Input::get('birthmonth'),
Input::get('company'),
Input::get('branches'),
Input::get('benefit'),
Input::get('designation'),
Input::get('tenure')
// Input::get('gender')
);
return Excel::create('data_function',function($excel) use ($users){
$excel->sheet('mysheet', function($sheet) use ($users){
$sheet->fromArray($users);
});
})->download('xls');
}
让我们这样做:
这对我来说很有效,所以我为你尝试了这个,请检查是否需要对你进行任何更改
//Initialize the array which will be passed into the Excel generator
$userarray= [];
// Define the Excel spreadsheet headers
$userarray[] = ['id', 'search','status','any_thing u_want'];
// Convert each member of the returned collection into an array,
// and append it to the payments array.
foreach ($users as $user) {
$userarray[] = $user->toArray();
}
// Generate and return the spreadsheet
Excel::create('payments', function($excel) use ($userarray) {
// Set the spreadsheet title, creator, and description
$excel->setTitle('users');
$excel->setCreator('Laravel')->setCompany('any_name, LLC');
$excel->setDescription('info file');
// Build the spreadsheet, passing in the payments array
$excel->sheet('sheet1', function($sheet) use ($userarray) {
$sheet->fromArray($userarray, null, 'A1', false, false);
});
})->download('xlsx');
}
帮助我,专家我只是在和任何人说话你怎么知道这个错误是由fromArray引起的?你认为错误在哪里@MahdiYounesi我认为您传递的是paginator对象
$users
而不是数组。$invoicesArray用于什么?很抱歉,我编辑了代码。请检查它是否对您有帮助。。。很抱歉格式不好。。。我不知道为什么它没有以不同的颜色显示注释我应该放什么来代替invoicearray?在loopreturn($userarray)之后检查userarray中的内容;并在开发人员模式ctrl+shift+i下检查它在网络选项卡中抛出的输出