Laravel Excel export with the header row

Today, I will give you an example of “Laravel Excel export with the header row”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, laravel 8, and Laravel 9 application.

First, what we’re doing here, This is the example :

Laravel Maatwebsite Excel Export with Header Cell

Laravel Excel export with the header row

Include column headers when exporting Eloquent to Excel in Laravel

Here, I will show you the cell maatwebsite in laravel. This tutorial will give you a simple example of laravel export with headings row maatwebsite. I would like to share with you laravel excel header.

Let’s start Laravel Excel export with the header row

resources\web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ExportController;


/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('driver-file-export', [ExportController::class, 'driverFileExport'])->name('driverFile-export');

resources\views\export.blade.php

 <a class="btn btn-success btn-sm" href="{{ route('driverFile-export') }}">Export data</a>

app\http\Controllers\ExportController.php

<?php

namespace App\Http\Controllers;

use App\Models\Driver;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\DriversExport;
use Illuminate\Http\Request;

class ExportController extends Controller
{

 
   public function driverFileExport(){
       return Excel::download(new DriversExport, 'drivers-collection.xlsx');
    }  
}

app\Exports\DriversExport.php

<?php

namespace App\Exports;

use App\Models\Driver;
use Maatwebsite\Excel\Concerns\WithHeadings;    
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\FromCollection;

class DriversExport implements FromCollection,WithHeadings,WithMapping
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Driver::select('id','driver_name','driver_phone')->get();
    }

    public function map($data): array
    {
        return[
        $data->id,
        $data->driver_name,
        $data->driver_phone
        ];
    }

    public function headings(): array
    {
        return [
            '#',
            'Driver Name',
            'Driver Phone Number'
        ];
    }
}


Related article:- Laravel – import excel sheet into the database in multiple tables.

Output:-

Laravel Excel export with the header row

In this article, we learned Laravel Maatwebsite Excel Export with Header Cell example, I hope this article will help you with your Laravel application Project.

Read also:- How to validate excel sheet data in laravel.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Scroll to Top