我有一个laravel函数,可将数据库查询导出到Excel。我正在使用Maatwebsite \ Excel \ Facades \ Excel。
问题是我试图将2个日期作为参数传递,但是当我这样做时,出现了Undefined variable错误。
我的问题是如何将变量从主函数传递给excel函数。这是我的代码:
public function excel2 (Request $request){
// These are the values I want pass to the query:
$initialDate=$request->get('initialDate');
$finalDate=$request->get('finalDate');
Excel::create('DataByDate',function($excel){
$excel->sheet('DataByDate',function($sheet){
$data =DB::select('SELECT c.cuenta,g.fecha as fecha, t.descripcion as tipificacion,
g.observacion as comentario, ROUND(NVL(c.saldoVencido,0),2) AS SaldoVencidoQ, ROUND(NVL(saldoVencidoD,0),2) AS SaldoVencidoD, ROUND(NVL(c.saldo,0),2) AS SaldoQ, ROUND(NVL(c.saldoD,0),2) AS saldoD,
ROUND(NVL(c.montoExtraf,0),2) AS MontoExtraf,c.cuotasVencidas AS CuotasVencidasQ, NVL(c.cuotasVencidasD,0) AS cuotasVencidasD, c.responsable AS Gestor from gestion g
inner join cliente c on g.idCliente = c.id
inner join tipificacion t on g.idTipif = t.id_tipif
where esPromesa!="R"
// This part is the one that is failing
and cast fecha as date between '.$initialDate.' and '.$finalDate.';
');
$data= json_decode( json_encode($data), true);
$sheet->fromArray($data,null,null,true);
});
})->export('xls');
}
如果需要在闭包范围内使用变量,则应使用该use()
语句。看起来像这样。
Excel::create('DataByDate', function ($excel) use ($initialDate, $finalDate) {
$excel->sheet('DataByDate', function ($sheet) use ($initialDate, $finalDate) {
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句