Search code examples
phplaravellaravel-excel

Laravel excel passing varable to exporter


I am working with Laravel Excel 3.1 trying to export file using two parameter

web.php {route file}

Route::post('/Download', 'Controller@Download');

and my controller

public function Download(Request $request)
{
$StartDate  =  Input::get('StartDate');
$EndDate = Input::get('EndDate');
$exporter = app()->makeWith(UsersExport::class, compact('StartDate','EndDate'));
return $exporter->download('Summary Detail.xlsx');
}

userExporter.php

class UsersExport implements FromQuery, WithHeadings,ShouldAutoSize
{
use Exportable;
protected $StartDate,$EndDate;
public function __construct(String  $StartDate,String $EndDate)
{
$this->StartDate = $StartDate;
$this->EndDate = $EndDate;
}
public function query()
{
return databaseReceipt::query()
->whereDate('created_at', '>=', $this->StartDate)
->whereDate('created_at', '<=', $this->EndDate)
->select('id','servicename',"created_at");
}
}

when I use static variable like "00:00 01/04/2017" and "00:00 01/01/2018" for start date & end date it works fine which lead me that passing variables doesn't work


Solution

  • sorry guys I figured out what was the problem << I think, I was tired that day be cause the solution is easy

    in function Download

    $S = date('Y-m-d H:i:s', strtotime(strtr($request->StartDate, '/', '-')));
    $E = date('Y-m-d H:i:s', strtotime(strtr($request->EndDate, '/', '-')));
    return (new UsersExport($S,$E))->download('invoices.xlsx');
    

    just to make it clear my mistake was because of different format date type. and by the way you can pass as much as you need of variable and any type