问题描述
我正在尝试从表中生成/下载excel,但是错误不断显示Call to a member function all() on array
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
class VehicleRequestExportDestination implements FromCollection
{
use Exportable;
public function collection()
{
$this->dateFrom = $this->dateFrom . ' 00:00:00.000';
$this->dateto = $this->dateto . ' 23:59:59.999';
$query = "
SELECT
SUBSTRING(destination,LOCATE('|',destination),LENGTH(destination)) AS dest,COUNT(destination) AS total
FROM dispatches
WHERE
addedDate
BETWEEN
'" . $this->dateFrom . "' AND '" . $this->dateto . "'
GROUP BY
destination
ORDER BY
total DESC
LIMIT 10";
return DB::select($query);
}
}
这就是我从控制器中调用的方式
return (new VehicleRequestExportDestination("2000-05-05","2030-05-05"))->download('Frequent Destination.xlsx');
是什么原因导致此问题,以及如何解决?
解决方法
似乎我缺少功能。我还需要将其转换为集合。此功能解决了我的问题。
return collect(DB::select($query));
不仅仅是返回
return DB::select($query);