如何在laravel控制器中设置变量并在导出到excel时使用该值
在我的控制器中,我需要连接一个字符串和一个变量。连接后,我将其设置为变量$datenow。导出到excel时,我希望将单元格A2的值设置为$datenow。但是我一直得到一个未定义变量的错误:datenow。这是我的密码:如何在laravel控制器中设置变量并在导出到excel时使用该值,laravel,laravel-5.5,maatwebsite-excel,Laravel,Laravel 5.5,Maatwebsite Excel,在我的控制器中,我需要连接一个字符串和一个变量。连接后,我将其设置为变量$datenow。导出到excel时,我希望将单元格A2的值设置为$datenow。但是我一直得到一个未定义变量的错误:datenow。这是我的密码: public function userSummary() { $mytime = Carbon\Carbon::now(); $datenow = 'As of'.' '.$mytime; Excel::create('Empoyee Summary
public function userSummary()
{
$mytime = Carbon\Carbon::now();
$datenow = 'As of'.' '.$mytime;
Excel::create('Empoyee Summary', function($excel) {
$excel->sheet('Sheet1', function($sheet) {
$sheet->setCellValue('A1', 'VCY Group of Companies');
$sheet->setCellValue('A2', $datenow);
});
})->export('xls');
}
如何将单元格A2的值设置为$datenow?试试这个
public function userSummary()
{
$mytime = Carbon\Carbon::now();
$datenow = 'As of'.' '.$mytime;
Excel::create('Empoyee Summary', function($excel) use ($datenow) {
$excel->sheet('Sheet1', function($sheet) use($datenow) {
$sheet->setCellValue('A1', 'VCY Group of Companies');
$sheet->setCellValue('A2', $datenow);
});
})->export('xls');
}
替换这一行-->Excel::create('Empoyee Summary',function($Excel)use($datenow)@Vipul它仍然说未定义变量:datenow它仍然是一个错误,说未定义变量:datenow它起作用了!但是如果我有其他变量怎么办?比如我将不仅仅使用$datenow。如果我有另一个变量,比如$name,应该使用它($datenow,$name)“@gwapo是的,你是这样用的