0

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

HelloWorld
  • 19
  • 1
  • 7
  • you can try my solution that is very simple and better way explained if any buddy so not use view method. https://stackoverflow.com/questions/62493600/laravel-excel-export-how-to-export-conditional-data/70275061#70275061 – pankaj Dec 08 '21 at 12:33

2 Answers2

1

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

HelloWorld
  • 19
  • 1
  • 7
-1
public function Download(Request $request) { $StartDate = Input::get('StartDate'); $EndDate = Input::get('EndDate'); $exporter = new UsersExport('StartDate','EndDate'); return $exporter->download('Summary Detail.xlsx'); }
AITL DEV11
  • 42
  • 1