How to Export Database Records to Excel using PHP(Laravel)

Have you encountered a challenge where you need to export records from the database to excel using Laravel?

We will be using Maatwebsite Laravel package

Installation

First require the Maatwebsite laravel package in your composer.json

c…

Have you encountered a challenge where you need to export records from the database to excel using Laravel?

We will be using Maatwebsite Laravel package



Installation

First require the Maatwebsite laravel package in your composer.json

composer require maatwebsite/excel

The Maatwebsite\Excel\ExcelServiceProvider is registered by default. But you can manually register the ServiceProvider in your config/app.php

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

The Excel Facade is discovered and registered by default. But you can add it manually to your config/app.php

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


Publish config by running the vendor command

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

This will create a new config file named config/excel.php. and your setup is completed.



Exporting

On our already seeded database, we will be exporting those records to an excel file.Checkout how to seed in laravel First we create a controller using:

 php artisan make:controller ExportToExcelController

then we create an export class in the app\Export using the:

php artisan make:export UsersExport --model=User

the UsersExport is the Export class while the flag --model=User is referencing the model class in use.

The result of the command php artisan make:export UsersExport --model=User:


<?php

namespace App\Exports;

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

class UsersExport implements FromCollection
{
    public function collection()
    {
        return User::all();
    }
}


Head over to your ExportToExcelController you can export records


<?php

namespace App\Http\Controllers;

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

class ExportToExcelController extends Controller 
{
    public function ExportRecords() 
    {
        return Excel::download(new UsersExport, 'users.xlsx');
    }
}


Ooh one more thing, add a route to be able to access ExportRecords:

Route::get('data/export/', 'ExportToExcelController@ExportRecords');

Thats IT! ?


Print Share Comment Cite Upload Translate
APA
John Samuel Obinna | Sciencx (2024-03-28T23:13:10+00:00) » How to Export Database Records to Excel using PHP(Laravel). Retrieved from https://www.scien.cx/2021/07/07/how-to-export-database-records-to-excel-using-phplaravel/.
MLA
" » How to Export Database Records to Excel using PHP(Laravel)." John Samuel Obinna | Sciencx - Wednesday July 7, 2021, https://www.scien.cx/2021/07/07/how-to-export-database-records-to-excel-using-phplaravel/
HARVARD
John Samuel Obinna | Sciencx Wednesday July 7, 2021 » How to Export Database Records to Excel using PHP(Laravel)., viewed 2024-03-28T23:13:10+00:00,<https://www.scien.cx/2021/07/07/how-to-export-database-records-to-excel-using-phplaravel/>
VANCOUVER
John Samuel Obinna | Sciencx - » How to Export Database Records to Excel using PHP(Laravel). [Internet]. [Accessed 2024-03-28T23:13:10+00:00]. Available from: https://www.scien.cx/2021/07/07/how-to-export-database-records-to-excel-using-phplaravel/
CHICAGO
" » How to Export Database Records to Excel using PHP(Laravel)." John Samuel Obinna | Sciencx - Accessed 2024-03-28T23:13:10+00:00. https://www.scien.cx/2021/07/07/how-to-export-database-records-to-excel-using-phplaravel/
IEEE
" » How to Export Database Records to Excel using PHP(Laravel)." John Samuel Obinna | Sciencx [Online]. Available: https://www.scien.cx/2021/07/07/how-to-export-database-records-to-excel-using-phplaravel/. [Accessed: 2024-03-28T23:13:10+00:00]
rf:citation
» How to Export Database Records to Excel using PHP(Laravel) | John Samuel Obinna | Sciencx | https://www.scien.cx/2021/07/07/how-to-export-database-records-to-excel-using-phplaravel/ | 2024-03-28T23:13:10+00:00
https://github.com/addpipe/simple-recorderjs-demo