Laravel excel 将变量传递给导出器

Laravel excel passing varable to exporter

我正在使用 Laravel Excel 3.1 尝试使用两个参数导出文件

web.php {路由文件}

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

和我的控制器

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");
}
}

当我使用像“00:00 01/04/2017”和“00:00 01/01/2018”这样的静态变量作为开始日期和结束日期时,它工作正常,这让我无法传递变量工作

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

抱歉,我想出了问题所在 << 我想,那天我很累,因为解决方案很简单

在函数下载中

$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');

只是为了澄清我的错误是因为格式日期类型不同。 顺便说一下,你可以根据需要传递尽可能多的变量和任何类型