몇 가지 값을 어레이로 전달하려고 합니다.jobExport()
수집 중 오류가 발생함Call to a member function jobsExport() on array
컬렉션이 모달 컬렉션 값으로 포퓰링해야 하는 것은 이해하지만 테이블에서 여러 레코드(선택한 레코드만)를 내보내려고 합니다.이렇게 하려면 컨트롤에서 모달 메서드로 값을 전달해야 하는데 이에 대한 해결책을 찾기 위해 전리품을 검색했지만 아직 찾을 수 없습니다.내가 한 일은 이렇다
경로
Route::any('export/jobs/{jobs}', [JobController::class, 'export']);
vue에서 larabel로 데이터 전달
watch: {
selected: function(){
this.url = '/export/jobs/' + this.selected;
}
}, // After sending
request on backend route will look like this http://127.0.0.1:8000/export/jobs/1,2,4
라라벨 컨트롤러
public function export($jobs)
{
return Excel::download(new JobsExport($jobs), 'jobs.xlsx');
}
모델 방법
public function jobsExport()
{
return Job::with('templates', 'teams')
->whereHas('templates', function ($q) {
$q->where('id', $this->id);
})
->get();
}
작업 내보내기
class JobsExport implements WithStyles, FromCollection, WithMapping, WithHeadings {
use Exportable;
private $jobs;
public function __construct($jobs)
{
$this->jobs = $jobs;
}
public function collection()
{
return $this->jobs->jobsExport();
}
public function map($jobsExport): array
{
// dd($jobsExport->templates->first()->template_name);
return [
$jobsExport->id,
$jobsExport->templates->implode('template_name', ', '),
$jobsExport->job_completed,
];
}
/**
* @return IlluminateSupportCollection
*/
public function headings():array
{
return[
'Id',
'Template',
'Completed',
];
}
}