Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Abdifatah's avatar

Laravel Excel Export Model with Relationship

This code only returns the Job Model only.


public function collection()
    {
        $jobs = Job::with("work")->get();
        return $jobs;
    }

I need each model rows with their corresponding child rows to appear inside it , just like nested table.

0 likes
3 replies
Abdifatah's avatar

Thanks @haz_ if you understand me , I want to export master detail


public function map($jobs): array
    {
        return [
            [
                $jobs->id,
                $jobs->position,
                $jobs->full_name,
                $jobs->mother_name
            ],
            [

                $jobs->work // how to loop work
            ],

            [

                $jobs->education
            ]

        ];

This works but work and education become one json excel column when I export it.

haztakki's avatar

Ah I misunderstood. What happens if you put them inside the first array with mother_name and the rest?

return [
[
 $jobs->I'd
 $jobs->position
 // etc
 $jobs->work,
 $jobs->education
]
]:

Please or to participate in this conversation.