Reputation: 125
i'm currently using version 3.1 of https://laravel-excel.com/ I want to query data and download them into excel file but it always returns a blank sheet.
my route:
Route::get('/reports/excel', 'ReportController@excel');
My ReportsExport Controller:
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class ReportsExport implements FromView, WithEvents, ShouldAutoSize
{
use Exportable;
private $from, $to;
public function __construct(String $from, String $to) {
$this->from = $from;
$this->to = $to;
}
public function view(): View
{
return view('reports.sample', [
'reports' => Report::whereBetween('created_at', [$this->from, $this->to])->get()
]);
}
download controller
public function excel(Request $request) {
$from = Carbon::parse($request->get('from'));
$to = Carbon::parse($request->get('to'));
return Excel::download(new ReportsExport($from, $to), 'reports.xlsx');
}
Can you please let me know what i'm missing here? Any help would be appreciated. Thank you!
Upvotes: 2
Views: 4285
Reputation: 125
I have managed to make it work. Here's what I did:
Exports.php
class ReportsExport implements FromView, WithEvents, ShouldAutoSize
{
use Exportable;
protected $from, $to;
public function __construct(String $from, String $to) {
$this->from = $from;
$this->to = $to;
}
public function view(): View
{
return view('reports.sample', [
'reports' => User::find(Auth::user()->id)->reports()
->whereDate('created_at', '>=', $this->from)
->whereDate('created_at', '<=', $this->to)
->get(),
]);
}
my Controller
public function excel(Request $request) {
$from = Carbon::parse($request->get('from'));
$to = Carbon::parse($request->get('to'));
return Excel::download(new ReportsExport($from, $to), 'reports.xlsx');
}
Upvotes: 2