We have the opportunity to download results as an excel-file like this: https://imgur.com/w1g3tnB But this doesn’t work here in Laravel 9.
This is how it looks in pdf: https://imgur.com/usjgBgw
I have replaced the create() to download() but hen I got the error
MaatwebsiteExcelExcel::download(): Argument #2 ($fileName) must be of type string, Closure given
in MaatwebsiteExcelRepository:
<?php
namespace AppRepositories;
use AppContractsExcelInterface;
use Excel;
class MaatwebsiteExcelRepository implements ExcelInterface
{
/**
* Creates a file based on given data.
*
* @param string $filename
* @param array $rows
* @param array $args
* @return object
*/
public function create($filename, $rows, $args = [])
{
$args['title'] = $this->getTitle($args);
$args['creator'] = $this->getCreator($args);
$args['company'] = $this->getCompany($args);
$args['description'] = $this->getDescription($args);
$args['keywords'] = $this->getKeywords($args);
$filenameParts = pathinfo($filename);
$filename = $filenameParts['filename'];
**COMMENT Next line gives the error**
return Excel::download($filename, function ($excel) use ($rows, $args) {
$excel->setTitle($args['title']);
$excel->setCreator($args['creator']);
$excel->setCompany($args['company']);
$excel->setDescription($args['description']);
$excel->setKeywords($args['keywords']);
if (count($rows) === 0) {
$rows[0] = [];
}
$excel->sheet('sheet1', function ($sheet) use ($rows) {
// Make the header row bold.
$sheet->cells('1:1', function ($cells) {
$cells->setFontWeight('bold');
});
if (! empty($rows[0])) {
// Make sure all columns in the rows are strings.
foreach ($rows as $rowIndex => $row) {
if (! empty($row) && is_array($row)) {
$rows[$rowIndex] = array_map('strval', $row);
} else {
$rows[$rowIndex] = $row;
}
}
}
$sheet->fromArray($rows);
});
// Create an empty second sheet, which is the default for most excel files.
$excel->sheet('sheet2', function ($sheet) use ($rows) {
});
});
}
So this:
return Excel::download($filename, function ($excel) use ($rows, $args)
must be rearranged in a way.
But how?
As before this works in Laravel 5.3