2

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!

1

1 Answer 1

2

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');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.