我正在嘗試將一些值作為陣列傳遞給jobExport()集合,但出現錯誤Call to a member function jobsExport() on array。我知道集合需要填充模態集合值,但我試圖從表中匯出多條記錄(僅我選擇的記錄),并且為了實作這一點,我需要將值作為陣列從控制元件傳遞給模態方法,我已經搜索了一個戰利品以找到解決方案,但我還沒有找到任何東西。這是我所做的
路線
Route::any('export/jobs/{jobs}', [JobController::class, 'export']);
將資料從 vue 傳遞到 laravel
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
Laravel 控制器
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 \Illuminate\Support\Collection
*/
public function headings():array
{
return[
'Id',
'Template',
'Completed',
];
}
}
uj5u.com熱心網友回復:
是$jobs身份證嗎?如果是這樣,做它$jobId
public function export($jobId)
{
// assuming you have Job model which holds the jobs table
$jobs = Job::where('id', $jobId)->get();
return Excel::download(new JobsExport($jobs), 'jobs.xlsx');
}
在你的出口班
class JobsExport implements WithStyles, FromCollection, WithMapping, WithHeadings
{
use Exportable;
private $jobs;
public function __construct($jobs)
{
$this->jobs = $jobs;
}
public function collection()
{
// change this
//return $this->jobs->jobsExport();
// to
return $this->jobs;
}
public function map($jobsExport): array
{
// dd($jobsExport->templates->first()->template_name);
return [
$jobsExport->id,
$jobsExport->templates->implode('template_name', ', '),
$jobsExport->job_completed,
];
}
/**
* @return \Illuminate\Support\Collection
*/
public function headings():array
{
return[
'Id',
'Template',
'Completed',
];
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/521864.html
