The best package to export data to excel in laravel

3 years ago SETHA THAY 2808
The best package to export data to excel in laravel

One of the default functionality that should exist in software or web development is exporting data in different formats such as Ms.Excel, PDF, or Ms.Word. It becomes a norm for almost every software or web application development. There are few benefits that why clients always demand that feature to existing. They can take the export data to perform a further calculation or sending as a report to the management team. Laravel doesn't come up with this feature by default, laravel developers have to explore various package to implement this functionality. 

If you are looking for a package to deal with data export to Ms. Excel in laravel, here you are, you can follow the example below to get your hand dirty with the exporting by using the package "maatwebsite/excel".
Let's dive in.

Step 1: Adding package to your project

composer require maatwebsite/excel

If you are using laravel sail, please use the below command

./vendor/bin/sail composer require maatwebsite/excel

Manually register the ServiceProvider Maatwebsite\Excel\ExcelServiceProvider in config/app.php

'providers' => [
    /*
     * Service Providers...
     */
    Maatwebsite\Excel\ExcelServiceProvider::class,
]

Then, add facade Excel in config/app.php

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

Publish config by running the command, and you will receive configuration file "config/excel.php"

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

Step 2: Exporting Your Data

  • Create Route and Controller
Route::get('employee/export', 'EmployeeController@exportRecords');
 php artisan make:controller EmployeeController
  • Create a table to input some sample records and then create a model associated with that created table
CREATE TABLE `tb_employee` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `gender` varchar(1) DEFAULT NULL,
  `address` varchar(500) DEFAULT NULL,
  `position` varchar(255) DEFAULT NULL,
  `salaray` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

best-package-to-export-data-to-excel-in-laravel-sample-records

php artisan make:model Employee

Then open Employee.php file and mapping to table "tb_employee"

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    use HasFactory;

    protected $table = 'tb_employee';
}
  • Make an export class using the artisan command

php artisan make:export EmployeeExport --model=Employee
<?php

namespace App\Exports;

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

class EmployeeExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Employee::all();
    }
}
  • Finally, create function to export data to excel format in controller
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exports\EmployeeExport;
use Maatwebsite\Excel\Facades\Excel;

class EmployeeController extends Controller
{
    //
    public function exportRecords(){
        return Excel::download(new EmployeeExport, 'employee.xlsx');
    }
}

Now, let open the browser and access the defined route, and you will see the excel file being download.

THANK YOU !!!!
Find Us @

Facebook
Telegram
Twitter
LinkedIn

 


About author

Author Profile

SETHA THAY

Software Engineer & Project Manager. I am willing to share IT knowledge, technical experiences and investment to financial freedom. Feel free to ask and contact me.



Scroll to Top