rap2hpoutre / fast-excel

🦉 Fast Excel import/export for Laravel
MIT License
2.09k stars 245 forks source link

Variables for collection #212

Closed marcelloinfoweb closed 3 years ago

marcelloinfoweb commented 3 years ago

Pass multiple variables with array for collection.

$chats = AssistemasChatQuery::whereIn('id', $searchResults[0]['AS_Sistemas_Chat'])->get();
$helps = AgromidiaHelpdeskQuery::whereIn('id', $searchResults[0]['Agromidia_Helpdesk'])->get();

$sheets = collect([$chats, $helps]);
(new FastExcel($sheets))->export('file.csv');

Erro Message: ErrorException Undefined variable: chats

marcelloinfoweb commented 3 years ago

I solved it with "if"

if (isset($searchResults[0]['AS_Sistemas_Chat'])) {
    $chats = AssistemasChatQuery::whereIn('id', $searchResults[0]['AS_Sistemas_Chat'])->get();
} else {
    $chats = [];
}
if (isset($searchResults[0]['Agromidia_Helpdesk'])) {
    $helps = AgromidiaHelpdeskQuery::whereIn('id', $searchResults[0]['Agromidia_Helpdesk'])->get();
} else {
    $helps = [];
}

$sheets = new SheetCollection([
    $chats,
    $helps,
]);

return (new FastExcel($sheets))->download('file.csv');