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

Maison012's avatar

Laravel collection Call to a member function exports() on array

I am trying to pass some values as array on jobExport() collection and am getting an error Call to a member function jobsExport() on array. I understand that the collection need to populatet with modal collection value, but am trying to export multiple record(only record i select) from table , and to make this happend i thing i need to pass value as array from control to modal method, i have searched a loot to find a solution for this but i dont find anythin yet. Here is what i have done

Route

Route::any('export/jobs/{jobs}', [JobController::class, 'export']);

Pass data from vue to 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 controller

public function export($jobs)
    {   
        return Excel::download(new JobsExport($jobs), 'jobs.xlsx');
    }

Model Method

public function jobsExport()
    {
        return Job::with('templates', 'teams')
            ->whereHas('templates', function ($q) {
                $q->where('id', $this->id);
            })
            ->get();
    }

JobExport

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',
        ];
    } 
}
0 likes
3 replies
AungHtetPaing__'s avatar
Level 22

@leon012 I don't have experienced with Laravel export but I think you can't call model method like this return $this->jobs->jobsExport(); because $jobs is not model instance right you just accept it as $jobs in controller.

Edited If you already have selected jobs id array, do it directly in JobExport instead of model.

// jobexport
    public function collection()
    {
        return Job::with('templates', 'teams')
            ->whereIn('id', $this->jobs)
            ->get();
    }

I hope I understood your question correctly. :)

2 likes
Maison012's avatar

@AungHtetPaing__ This is what i was looking foor , u was tyrinmg to pass selected array on modal before. But now with this method work perfect

// I just need to send jobs as array so i have added `explode`
public function export(Request $request)
    {
        $job = $request->jobs;
        $jobs = explode(',', $job);
        
        return Excel::download(new JobsExport($jobs), 'jobs.xlsx');
    }
}
1 like
NoLAstNamE's avatar

Is the $jobs an Id? If so, make it $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');
}
1 like

Please or to participate in this conversation.