load the required related data in the constructor
public function __construct(Job $jobs)
{
$jobs->load('templates','teams');
$this->jobs = $jobs;
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Want to export my data from table on excel (xls) format. But i have some model relationship there, and for now i can just ecport only one modal data.
Here is the export method
public function export(Job $jobs)
{
return Excel::download(new JobsExport($jobs), 'jobs.xlsx');
}
job model
public function jobsExport(){
return Job::with(['templates', 'teams'])->get();
}
also export.php
class JobsExport implements FromCollection, WithMapping, WithHeadings
{
use Exportable;
private $jobs;
public function __construct(Job $jobs)
{
$this->jobs = $jobs;
}
public function collection()
{
return $this->jobs->jobsExport();
}
public function map($jobsExport): array
{
return [
$jobsExport->job_name,
$jobsExport->templates->template_name, //i cant access this
$jobsExport->job_description,
$jobsExport->job_completed,
];
}
/**
* @return \Illuminate\Support\Collection
*/
public function headings():array
{
return[
'Id',
'Created_at',
'Updated_at'
];
}
}
Jobtypes and teams canot exported there is my problem
Please or to participate in this conversation.