Php Laravel/LaraCSV-每个之前的全局变量
我的控制器中有一个函数,我试图使用LaraCSV导出一个.csv文件。这是他们的可用文档:。无论出于何种原因,我似乎无法将我之前在函数中声明的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
$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;
});