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

username1's avatar

Export file to excel, not getting filtered data

good day everyone, I would like to export file to excel with my filtered data, but It's not working it turns out fetching all data from database not filtered data. please help me I don't know what to do next. im just new in livewire

here is my component ,,, public function printExcel() {

     return Excel::download(new JobOrderExport, 'ReportListing.xlsx');
}

here is my export class ,,, public function view(): View { $job_status = $this->status; $work_typ = $this->job_type_id; $date_start = $this->start_date; $date_end = $this->end_date;

    $report_list = JobOrder::where(function($query) use ($job_status, $work_typ, $date_start, $date_end){

                                if($job_status) {
                                    $query->where('status', $job_status);
                                } 
                                if($work_typ) {
                                    $query->where('job_type_id', $work_typ);
                                }
                                if($date_start && $date_end) {
                                    $query->where('created_at', '>=',$date_start)
                                            ->where('created_at', '<=',$date_end);
                                }                                                                                                    
                            })->get();           
   dd($report_list);
    return view('layouts.exports.job-report-listing',[
        'report_listing' => $report_list
    ]);
}
0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

And how are you passing the data to the Export class?

You cannot use $this-> within the export class and expect to use data from your livewire component, you'll have to pass it to the export class.

So in your Export class add this:

public $jobStatus;
public $workType;
public $startDate;
public $endDate;

public function __construct($jobStatus, $workType, $startDate, $endDate)
{
    $this->jobStatus = $jobStatus;
    $this->workType = $workType;
    $this->startDate = $startDate;
    $this->endDate = $endDate;
}

public function view(): View {

    $report_list = JobOrder::when($this->jobStatus, function($query) {
            $query->where('status', $this->jobStatus);
        })
		->when($this->workType, function($query) {
            $query->where('job_type', $this->workType);
        })
		->when($this->startDate && $this->endDate, function($query) {
            $query->where('created_at', '>=',$this->startDate)
                ->where('created_at', '<=', $this->endDate);
        })
		->get();           
   dd($report_list);
    return view('layouts.exports.job-report-listing',[
        'report_listing' => $report_list
    ]);
}

And in your component

return Excel::download(new JobOrderExport($this->status, $this->job_type_id, $this->start_date, $this->end_date), 'ReportListing.xlsx');

I tried to use your variables, but ofc make sure you use the correct ones. But that's the process.

1 like

Please or to participate in this conversation.