ItSolutionStuff.com

How to add header row in export excel file with maatwebsite in Laravel 5.8?

By Hardik Savani • November 5, 2023
Laravel

Laravel maatwebsite/excel introduce new version 3 with new feature and code structure. If you see on quick example of maatwebsite then it will very easy to export excel or csv. but if you want add headings row with export excel file then you can do it using WithHeadings Class. So you can add export csv with headers cell maatwebsite in laravel 5.8.

I will simple show you how you can set specific header on your export excel file using maatwebsite/excel composer package. If you want to import export from scratch then you can follow this tutorial: Import Export CSV File in Laravel 5.8.

So, let's see bellow example how it is done. just follow bellow file:

app/Exports/ServiceExport.php

<?php

namespace App\Exports;

use App\Service;

use Maatwebsite\Excel\Concerns\FromCollection;

use Maatwebsite\Excel\Concerns\WithHeadings;

class ServiceExport implements FromCollection, WithHeadings

{

/**

* @return \Illuminate\Support\Collection

*/

public function collection()

{

return Service::all();

}

public function headings(): array

{

return [

'Code',

'Description',

'Pos',

'Mod A',

'Mod B',

'Charge',

];

}

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Task Scheduling with Cron Job in Laravel 5.8

Read Now →

Laravel 5.8 Email Verification Example

Read Now →

How to create 404 error page in Laravel 5.8?

Read Now →

Build RESTful API In Laravel 5.8 Example

Read Now →

How to create and download pdf in Laravel 5.8?

Read Now →

Ajax Autocomplete Textbox in Laravel 5.8 Example

Read Now →

Laravel 5.8 CRUD (Create Read Update Delete) Tutorial For Beginners

Read Now →