Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在laravel控制器中设置变量并在导出到excel时使用该值_Laravel_Laravel 5.5_Maatwebsite Excel - Fatal编程技术网

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

如何在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

在我的控制器中,我需要连接一个字符串和一个变量。连接后,我将其设置为变量$datenow。导出到excel时,我希望将单元格A2的值设置为$datenow。但是我一直得到一个未定义变量的错误: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');      
}

替换这一行-->Excel::create('Empoyee Summary',function($Excel)use($datenow)@Vipul它仍然说未定义变量:datenow它仍然是一个错误,说未定义变量:datenow它起作用了!但是如果我有其他变量怎么办?比如我将不仅仅使用$datenow。如果我有另一个变量,比如$name,应该使用它($datenow,$name)“@gwapo是的,你是这样用的