Laravel 9 Export data in excel and csv file using laravel

By Guest | Mar 15, 2022 | Laravel
Share : Whatsapp

https://www.fundaofwebit.com/post/how-to-export-data-in-excel-file-using-laravel-9

Laravel 9 Export data in excel and csv file using laravel from database.


In this tutorial, you will be learning how to export data to excel file in laravel 9. So to export data in excel and csv in Laravel, use the maatwebsite/excel 3.1 package. So start with Laravel Excel Export.


Step 1: Install maatwebsite/excel Package with following command:

composer require psr/simple-cache:^1.0 maatwebsite/excel

If you are using less than laravel 9 version, then install with below command:

composer require maatwebsite/excel

If you are using less than laravel 9 version, lets add it manually in the following path: config/app.php and add service provider and alias:

'providers' => [
    ....
   Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [
    ....
   'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],

after adding the provider and aliases, run the below command to pushlish.

php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config


Step 2: Create Export Class with the following command:

php artisan make:export StudentsExport


Step 3: After Export created successfully, Open StudentsExport.php file in the following path: app/Exports/StudentsExport.php and paste the below code:

<?php

namespace App\Exports;

use App\Models\Student;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class StudentsExport implements FromView
{
    public function view(): View
    {
        return view('exports.studentexport', ['students' => Student::all()]);
    }
}

Step 4: Create a file named studentexport.blade.php in following path: resources/views/exports/studentexport.blade.php and paste below code:

This file is going to be converted in Excel Sheet when you export data. This file is called in Step 3.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Export Student Data</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Course</th>
                <th>Created At</th>
            </tr>
        </thead>
        <tbody>
            @foreach($students as $student)
            <tr>
                <td>{{ $student->id }}</td>
                <td>{{ $student->name }}</td>
                <td>{{ $student->email }}</td>
                <td>{{ $student->phone }}</td>
                <td>{{ $student->course }}</td>
                <td>{{ $student->created_at->format('d-m-Y') }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
</body>
</html>


Step 5: Create a controller eg: StudentController.php and paste the below code in it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Exports\StudentsExport;
use Maatwebsite\Excel\Facades\Excel;

class StudentController extends Controller
{
    public function index()
    {
        return view('student.view');
    }

    public function exportExcel()
    {
        return Excel::download(new StudentsExport, 'students.xlsx');
    }
}


Step 6: Define the Routes in following path:

Route::get('students', [App\Http\Controllers\StudentController::class, 'index']);
Route::get('export-student', [App\Http\Controllers\StudentController::class, 'exportExcel']);


Step 7: Create a file view.blade.php in the path: resources/views/student/view.blade.php and paste the below code:

@extends('layouts.app')

@section('content')

    <div class="container mt-4">
        <div class="card">
            <div class="card-header">
                <h4>Export Excel / CSV File data in Laravel</h4>
            </div>
            <div class="card-body">

                <a href="{{ url('export-student') }}" class="btn btn-primary">Export Student Data</a>

            </div>
        </div>
    </div>

@endsection


Step 8: That's it, you are done, Now serve the application.

php artisan serve


Once you download the excel format will be:



Thank you