1
votes

Basically i got a query and the results will be created as an excel file. Here is my code

$data = \DB::table('checkers')
        ->where('remarks_id',2)
        ->join('schedules','schedules.id','=','checkers.schedule_id')
        ->join('teachers','schedules.teacher_id','=','teachers.id')
        ->join('subject_codes','subject_codes.id','=','schedules.subject_code_id')
        ->join('remarks','remarks.id','=','checkers.remarks_id')
        ->where('checkers.created_at', '=>', $request->from)
        ->where('checkers.created_at', '=<', $request->to)
        ->select('teachers.fullname','subject_codes.subject_description','remarks.remarks_desc','checkers.created_at')
        ->get(); 

        $data = $data->map(function ($x) {
            return json_decode(json_encode($x), true);
        })->all();

        // dd($data);

        Excel::create('report', function($excel) use($data) {
            $excel->sheet('Sheetname', function($sheet) use($data) {
            });
        })->export('xls');

when i do `dd($data)

array:2 [
  0 => {#294
    +"fullname": "Edcel Pagagao"
    +"subject_description": "Capstone 1"
    +"remarks_desc": "No Teacher Around"
    +"created_at": "2020-01-30 00:00:00"
  }
  1 => {#298
    +"fullname": "Edcel Pagagao"
    +"subject_description": "Capstone 1"
    +"remarks_desc": "No Teacher Around"
    +"created_at": "2020-01-27 00:00:00"
  }
]

Which is what the docs said https://docs.laravel-excel.com/2.1/export/array.html based on 2.x version

But i still got error continue" targeting switch is equivalent to "break". Did you mean to use "continue 2

1
Is there some code inside ->sheet(... ? - Felippe Duarte
what you mean sir @FelippeDuarte - draw134
I mean if this is your real code or if there is more code where you actually put the $data inside your sheet. - Felippe Duarte
i am using use($data) - draw134
why you do this part return json_decode(json_encode($x), true); - Hamelraj

1 Answers

1
votes

As per you link Link first example says you need to pass array data like below

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

you can change collection as array using toArray()

$data = \DB::table('checkers').....->select('teachers.fullname','subject_codes.subject_description',
'remarks.remarks_desc','checkers.created_at')
->get()
->toArray();