Search code examples
laravelmaatwebsite-excel

implementing maatwebsite headings using foreach


i want to ask about how to implement multiple heading in maatwebsite

here is may code

public function headings(): array

{

    $mapel = DB::table('tb_mapel')->select('nama_mapel')->get()->toArray();

    return [
        ['LEGER'],
        ['', '', 'KI3 (Pengetahuan)'],
        ['NIS', 'Nama Siswa', $mapel],
    ];
}

this is the the result of code:

enter image description here

And this is what i want

enter image description here


Solution

  • To get only the values of nama_mapel, use pluck()

    Then add the two other elements at the start of the array using array_unshift()

    $mapel = DB::table('tb_mapel')->pluck('nama_mapel')->toArray();
    array_unshift($mapel, 'NIS', 'Nama Siswa');
    
    return [
            ['LEGER'],
            ['', '', 'KI3 (Pengetahuan)'],
            $mapel,
        ];