jeudi 4 août 2022

How to export currently searched data in excel laravel?

I am trying to exceldownload only the data searched by the user but my code isnt working.

Controller

 public function get_data(Request $request, Covidrecord $covidrecord)
{
    $search = $request['search'] ?? '';
    if ($search != '') {
        $covidrecord = Covidrecord::where('fullname', 'LIKE', "%$search%")
            ->orWhere('gender', 'LIKE', "%$search%")
            ->orWhere('age', 'LIKE', "%$search%")
            ->orWhere('hospital_type', 'LIKE', "%$search%")
            ->orWhere('vaccine_status', 'LIKE', "%$search%")
            ->get();
    }
   
       Excel::create('covidrecord', function($excel) use($covidrecord) {
                   $excel->sheet('sheet 1', function($sheet) use($covidrecord){
                       $sheet->fromArray($covidrecord);
                   });
       })->download('xlsx');

}

Below is the Export.php..How can i filter search here??

CovidRecordExport.php

class CovidRecordExport implements FromCollection
{
   
    public function headings():array{
        return[
            'fullname',
            'age',
            'gender',
            'ethinicity',
            'religion',
            'occupation',
            'qualification',
            'financial_status',
            'marital_status',
            'infection_date',
            'covid_verficiation',
            'hospitalization_date',
            'hospital_type',
            'death_date',
            'death_reason',
            'after_covid_death',
            'death_location',
            'vaccine_status',
            
        ];
    }
    public function collection()
    {
        return CovidRecord::all();
    }
}

I tried dd() and it seems like my if condition is not working.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/rxQNXY5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire