0
votes

I want to export the data from my database to excel using laravel excel maatwebsite, the problem is i don't know how to add index column to the excel file. for example i want the data in excel from this look:

Name        Birthdate
Rudolf      1990-05-20
Andi        1991-11-01
Harry       1993-03-25

to this:

No      Name        Birthdate
1       Rudolf      1990-05-20
2       Andi        1991-11-01
3       Harry       1993-03-25

this is my model:

namespace App\Exports;

use App\user;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

class userExport implements FromQuery, WithHeadings, ShouldAutoSize{
    use Exportable;

    public function headings(): array{
        return [
            'Tanggal',
            'Nomor Permohonan',
            'Nama Pemohon',
        ];
    }
    public function __construct(int $status)
    {
        $this->status = $status;
    }

    public function query(){
        $date   = \Carbon\Carbon::now()->toDateString() ;
        return user::query()->select('name','birthdate')->where('status', $this->status)->whereDate('date',$date);
    }
}

My function in controller:

public function export() {
        return (new userExport(1))->download('user.xlsx');
}

How can i do that?, help me. Thank you.

1
What about adding it to the public function query?Douwe de Haan

1 Answers

0
votes

You have to try select id in your query user::query()->select('id', 'name','birthdate')