K
K
Konstantin P2020-08-02 17:20:33
Laravel
Konstantin P, 2020-08-02 17:20:33

How to create a new column when exporting a table using Laravel, maatwebsite/excel?

Hello! Can you please tell me how to create a new column in the exported (in excel) table? There is a table in the database of the following form:
5f26ca148223d812665543.png
I installed a package for exporting maatwebsite/excel using composer. There is also a model file:

class ScheduledInspectionModel extends Model
{
    protected $table = 'scheduled_inspection'; // название таблицы
    protected $fillable = ['name_smp', 'name_control', "verification_start", "verification_end", 'verification_duration'];
    public $timestamps = false;
}

Controller:
class OrganizationsExportController extends Controller
{
    public function export()
    {
        return (new OrganizationsExport)->download('organizations_export.xls');
    }
}

And the export description file:
class OrganizationsExport implements FromCollection, ShouldAutoSize, WithHeadings, WithEvents
{
    use Exportable;
    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return ScheduledInspectionModel::all();
    }

    public function headings(): array
    {
        return [
            'id',
            'Проверяемый СМП',
            'Контролирующий орган',
            'Начало проверки',
            'Окончание проверки',
            'Плановая длительность'
        ];
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $event->sheet->getStyle('A1:F1')->applyFromArray([
                    'font' => [
                        'bold' => true
                    ]
                ]);
            }
        ];
    }

The exported table looks like this:
5f26cacfa9f25491944233.png
Export works :) But I want to make sure that instead of the id column (I can exclude it using map() and in the Model ), there is a 'Number' column and enter line numbering accordingly. Please tell me how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin P, 2020-08-02
@kost_p

added/updated to the OrganizationsExportfollowing:

...
private $count = 0;

    public function map($organizations): array
    {
        return [
            ++$this->count,
            $organizations->name_smp,
            $organizations->name_control,
            $organizations->verification_start . ' - ' . $organizations->verification_end,
            $organizations->verification_duration,

        ];
    }

    public function headings(): array
    {
        return [
            '№',
            'Проверяемый СМП',
            'Контролирующий орган',
            'Период плановой проверки',
            'Плановая длительность',
        ];
    }
...

Here is how in DB:
5f2710b3ca8a4246567295.png
Here is how in excel file:
5f2710c14baa5551548549.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question