Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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
Php Laravel/LaraCSV-每个之前的全局变量_Php_Laravel - Fatal编程技术网

Php Laravel/LaraCSV-每个之前的全局变量

Php Laravel/LaraCSV-每个之前的全局变量,php,laravel,Php,Laravel,我的控制器中有一个函数,我试图使用LaraCSV导出一个.csv文件。这是他们的可用文档:。无论出于何种原因,我似乎无法将我之前在函数中声明的$doctor变量用于LaraCSV的beforeach函数。有人知道如何做到这一点吗?或者我需要执行另一个where()find from patient to the doctor public function doctorCSV($id) { $csvExporter = new \Laracsv\Export(); $doctor

我的控制器中有一个函数,我试图使用LaraCSV导出一个.csv文件。这是他们的可用文档:。无论出于何种原因,我似乎无法将我之前在函数中声明的
$doctor
变量用于LaraCSV的
beforeach
函数。有人知道如何做到这一点吗?或者我需要执行另一个
where()
find from patient to the doctor

public function doctorCSV($id) {
    $csvExporter = new \Laracsv\Export();
    $doctor = Doctor::findOrFail($id);
    $scripts = $doctor->scripts()->get();

    $csvExporter->beforeEach(function ($script) {
        $patients = Patient::all();
        $patient = $patients->where('id', $script->patient_id)->first();
        $script->patient = $patient->full_name;
        $script->doctor = $doctor->full_name;
    });

    return $csvExporter
        ->build($scripts, [
            'prescribe_date' => 'Prescribe Date',
            'doctor' => 'Doctor',
            'patient' => 'Patient',
            'status' => 'Status'
        ])
        ->download('Report - ' . $doctor->full_name . ' - ' . date('m-d-Y') . '.csv');
}

像这样使用php
Closure
语句

$csvExporter->beforeEach(function ($script) use($doctor) { // here
        $patients = Patient::all();
        $patient = $patients->where('id', $script->patient_id)->first();
        $script->patient = $patient->full_name;
        $script->doctor = $doctor->full_name;
    });