Home > Net >  How to make dynamic header data export from database to excel using Maatwebsite in Laravel
How to make dynamic header data export from database to excel using Maatwebsite in Laravel

Time:01-05

In my case, i have problem like this

Sample table and data :

// Table Account
Id  |  Name  |
 1  |  Kiara |
 2  |  Steve |

My currently code is :

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use App\Models\Account;

class ExportTable implements FromCollection
{
    public function collection()
    {
        return Account::all();
    }
}

What i had now in excel :

 1  |  Kiara |
 2  |  Steve |

I want show the headers value as dynamic as table, without more effort like this

    public function headings(): array
    {
        return [
                "Id",
                "Name"  
        ];
    }

Is it posible?

CodePudding user response:

 public function headings(): array
{
    return Illuminate\Support\Facades\Schema::getColumnListing('tableName');

}

Try this

CodePudding user response:

add your model,

public function getTableColumns() 
{
    return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
}

and ...

public function headings(): array
{
   $model = new Model();

   $columns = $model->getTableColumns();
   return $columns;
}

try this cok.

  • Related