如何在 laravel 控制器中设置变量并在导出到 excel 时使用该值

How to set a variable in laravel controller and use the value in exporting to excel

在我的控制器中,我需要连接一个字符串和一个变量。在我连接它之后,我将它设置为变量 $datenow。当我导出到 excel 时,我想将 cell A2 的值设为 $datenow。但是后来我不断收到 Undefined variable: datenow 的错误。这是我的代码:

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