laravel - 导出到 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'),
            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');
}

关于laravel - 导出到 Excel 时出现非法偏移类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393467/

相关文章:

php - Laravel 中的 Ajax 419 状态错误

php - Laravel 5 路由组和组内的 Basic(/) GET 路由

php - Laravel 5 加入表并选择热门类别

laravel-5.5 - Laravel 5.5 - 通知没有排队?

javascript - Laravel 5.5 Invisible Recaptcha 不提交表单

laravel - 使用 Docker 容器连接到 Laravel 中的 Postgres

sql - Laravel Eloquent ORM "whereHas"通过表

php - 当用户离开页面时使 Laravel 中的登录无效

php - 未指定应用程序加密 key

laravel - 自定义 Laravel 迁移命令 "[Illuminate\Database\Migrations\MigrationRepositoryInterface] is not instantiable"