2

I'm trying to export an excel but when i export it, it gives me an error of Illegal offset type error. How can i make it work? Heres my code in my 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');
    }
14
  • Help me experts Commented Jan 23, 2018 at 3:03
  • Hi I'm just talking to no one Commented Jan 23, 2018 at 3:08
  • How do u know the error belongs ro tgis code Commented Jan 23, 2018 at 3:35
  • Because of the fromArray? Where do you think is the error? @MahdiYounesi Commented Jan 23, 2018 at 3:38
  • 1
    I think that You are passing paginator object $users not array. Commented Jan 23, 2018 at 4:44

1 Answer 1

3

Lets do this :

this worked for me so i tried this for u please check if any change is needed for u .

        //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');
}
Sign up to request clarification or add additional context in comments.

6 Comments

whats the $invoicesArray for?
sorry for that i have edited code . please check if it is any help to you ... and sorry for poor format ... i dont know why it's not showing comment in different color
what should i put instead of invoicearray?
check what is inside in userarray after loop
return ( $userarray ); and check output it throws in network tab in developer mode ctrl+shift+i
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.