class FollowupExport implements FromQuery, WithHeadings
{
use Exportable;
public $patient_id; //HERE
public function __construct($patient_id)
{
$this->patient_id = $patient_id;
}
/**
* @return \Illuminate\Support\Collection
*/
public function query()
{
return Followup::query()->where('patient_id', $this->patient_id); //HERE
}
public function headings(): array
{
return [
'id no',
'created_by',
'patient_id',
'fu_date',
'weight',
'waist_circum',
'hip_cirum',
'systolic',
'diastolic',
'pulse_rate',
];
}
}
How to export data with Maatwebsite laravel excel with where condition
Hello,
I have exported data with Maatwebsite laravel excel easily for normal. But I could not export the data for where condition. The parameter should come from client side /api/followup/export/${id} the id is patient id, not followup id.
The excel file is downloaded but there is no data at all. But there is title headers. I think where condition is not match or parameter id is not passed.
I've used Vue.js at the front end. The button to download is like this
<button class="btn btn-secondary download " type="button" @click.prevent="download(this.$route.params.id)">Export data in Excel </button>
at script
methods: {
download(id) {
window.location.href = `/api/followup/export/${id}`;
},
}
Or How to send this id parameter to the controller in this condition.
I want to export data for this ->
public function showPatientFollowupList($id)
{
return Followup::where('patient_id', $id)->get();
}
In controller for export I have this
public function export($patient_id)
{
// return Excel::download(new FollowupExport, 'Followup-patients-list.xlsx');
return (new FollowupExport($patient_id))->download('Followup-patients-list.xlsx');
}
In route Route::get('followup/export/{id}', [FollowupController::class, 'export']);
In FollowupExport.php
namespace App\Exports;
use App\Models\Record\Followup;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\Exportable;
// use Illuminate\Support\Facades\DB;
class FollowupExport implements FromQuery, WithHeadings
{
use Exportable;
public function __construct($patient_id)
{
$this->patient_id = $patient_id;
}
/**
* @return \Illuminate\Support\Collection
*/
public function query()
{
return Followup::query()->wherePatient_id('patient_id', '$this->patient_id');
}
public function headings(): array
{
return [
'id no',
'created_by',
'patient_id',
'fu_date',
'weight',
'waist_circum',
'hip_cirum',
'systolic',
'diastolic',
'pulse_rate',
];
}
}
Please help me out.
change your vue code
window.location.href='/api/followup/export/' + id;
I suggest you also give some consideration for data privacy since anyone can load this route and download data for any id
Please or to participate in this conversation.