Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given

人走茶凉 提交于 2020-08-11 04:59:34

问题


Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given, called in C:\xampp\htdocs\student_route\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 237

public function excel_report()
    {
     $student_data = DB::table('student_details')->get()->toArray();
     $student_array[] = array('Name', 'Address', 'Roll No', 'Class');
     foreach($student_data as $student)
     {
      $student_array[] = array(
       'Student Name'  => $student->st_name,
       'Address'   => $student->address,
       'Roll No'   => $student->roll_no,
       'Class'    => $student->st_class
      );
     }
     Excel::download('Student_Data', function($excel) use ($student_array){
      $excel->setTitle('Student Datas');
      $excel->sheet('Student_Datass', function($sheet) use ($student_array){
       $sheet->fromArray($student_array, null, 'A1', false, false);
      });
     })->download('xlsx');
    }

I got error Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given.don't know where is issue. anyone check please. i am using laravel 5.8


回答1:


I haven't use that package, but you can see from the method signature that the second argument expects a string that will be the name of the file to download:

public function download($export, string $fileName, string $writerType = null, array $headers = [])

In your case, you are returning a callback.


Update 1

Seeing the docs, I think that you what you are trying to use the static create method of the Excel class, but instead you are using (incorrectly) the download one. From the docs:

Creating a sheet from an array

Array

To create a new file from an array use ->fromArray($source, $nullValue, $startCell, $strictNullComparison, $headingGeneration) inside the sheet closure.

Excel::create('Filename', function($excel) {

    $excel->sheet('Sheetname', function($sheet) {

        $sheet->fromArray(array(
            array('data1', 'data2'),
            array('data3', 'data4')
        ));

    });

})->export('xls');

So, in your case, replace the first download](...) with create(...):

public function excel_report()
{
    $student_data = DB::table('student_details')->get()->toArray();
    $student_array[] = array('Name', 'Address', 'Roll No', 'Class');

    foreach ($student_data as $student)
    {
        $student_array[] = array(
            'Student Name' => $student->st_name,
            'Address'      => $student->address,
            'Roll No'      => $student->roll_no,
            'Class'        => $student->st_class
        );
    }

    Excel::create('Student_Data', function ($excel) use ($student_array) {
//        ^^^^^^^
        $excel->setTitle('Student Datas');
        $excel->sheet('Student_Datass', function ($sheet) use ($student_array) {
            $sheet->fromArray($student_array, null, 'A1', false, false);
        });
    })->export('xls');
//    ^^^^^^^^^^^^^^
}

Update 2

The above code works for v2, but given that you are using the v3 this can't help you. In the current version, you can format your output file with a combination of Exports and Sheet classes. Actually, this is more modular approach to decouple and improve your code. Check this section of the docs.




回答2:


As of maatwebsite/Laravel-Excel 3.1

Excel::download method's signature is changed to

public function download($export, string $fileName, string $writerType = null, array $headers = [])

download expects 2nd argument as string, callback is no longer there. see this upgrade guide here

If you want to keep using sane behavior of v2.1. you can use this package convenia/excel

as an alternative.

This helps when you want to keep up to date with latest laravel versions without making changes to your import/ export code.

If you have less import / export functionalities then I would recommend to change it according to maatwebsite/Laravel-Excel 3.1



来源:https://stackoverflow.com/questions/56202056/argument-2-passed-to-maatwebsite-excel-exceldownload-must-be-of-the-type-str

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!