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

bhojkamal's avatar

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.

0 likes
9 replies
Snapey's avatar
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',
		];
	}
}
bhojkamal's avatar

@Snapey

Thank your for your reply, I've tried it, but still, it is not working. The excel file is downloaded as before with title but no data at all.

Snapey's avatar

where you call the export, pass the patient_id as a hardcoded value , then you will know if the problem it's with the frontend or laravel-excel

Snapey's avatar
Snapey
Best Answer
Level 122

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

bhojkamal's avatar

@Snapey

Thank you very much

window.location.href='/api/followup/export/' + id;

This worked, and even when I hardcoded on FollowupExpert.php as below,

public function query()
    {
        return Followup::query()->where('patient_id', 3);
    }

If this is not better way, please suggest me the better way too. You have helped me to solve the issue, I will give the best answer. For security, it is checked in the my controller.

Snapey's avatar

@bhojkamal obviously you need to pass the id from the front to the export. By hardcoding the value you split the problem in two making it easier to diagnose where the issue is

Harisul_Ishbah's avatar

@bhojkamal @snapey Can you guys help me, why ajax is not working : View : $('#export').click(function (e) { let rows_selected = tblMassy.column(0).checkboxes.selected(); if (rows_selected.length >= 1) {

                        $.each(rows_selected, function(index, rowId){

                            console.log( "http://localhost:8000/eksportfilter/" + rowId ) ;
                            $.ajax({
                                type:'POST',
                                url: "http://localhost:8000/eksportfilter/" + rowId,
                                dataType: "JSON",
                                success: function (response) {
                                    console.log( response )
                                }
                            });
                            
                        });
                            console.log(rows_selected.length);
                            swal("Good job!", "Eksport data successfully!", "success");
                            $('#tblMassy').DataTable().ajax.reload();
                    } else {
                        swal("Failed Export!", "No Data Delected!", "error");
                    }
    });

Route : Route::post('eksportfilter/{id}', [MAssyController::class , 'eksportfilter'])->name('eksportfilter.post');

Controller : public function eksportfilter($id) { return (new TransactionsExportFilter($id))->download('Followup-patients-list.xlsx'); }

Class Export : class TransactionsExportFilter implements FromQuery, WithHeadings { use Exportable;

public $id;   //HERE

public function __construct($id)
{
    $this->id = $id;
}

/**
* @return \Illuminate\Support\Collection
*/
public function query()
{
    return m_assy::query()->where('id', $this->id);  //HERE
}

public function headings(): array
{
    return [
        'id',
        'assymainharness',
        'assyab',
        'assyab1',
        'assyab2',
        'assyab3',
        'type',
        'assycode',
        'suffixlevel',
        'desccarline',
        'factory',
        'customer',
    ];

} }

Snapey's avatar

@Harisul_Ishbah you are calling ajax endpoint for every single row?

what are you expecting to happen?

why are you doing this as an ajax request?

Sinnbeck's avatar

@Harisul_Ishbah it might be a good idea to create your own thread so we can help you without spamming the original author of the thread

1 like

Please or to participate in this conversation.