excel file create in laravel
SourceURL:file:///home/wadmin/Desktop/LARAVEL NOTES/create Excel in Laravel .docx
we can export any table data into excel ... using package ‘maatwebsite/excel’
composer require maatwebsite/excel |
now include this command:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config |
make a controller wth name ‘UserController’
php artisan make:controller UsersController |
now, we will paste the code .. into controller
use Excel; use App\Exports\ExportUsers;
public function exportUsersData(){ $fileName = 'users.xlsx'; return Excel::download(new ExportUsers, $fileName); } |
make a folder ‘Exports’ inside /App directory. and create a file as ‘ app/Exports/ExportUsers.php’
now paste the code ...
<?php
namespace App\Exports;
use DB;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class ExportUsers implements FromCollection, WithHeadings {
public function headings(): array { return [ "id", "name", "date" ]; }
public function collection(){ $usersData = DB::table('invalidemails')->select('id','name','date')->get(); return collect($usersData);
}
} |
now we will export column of ‘id’,’name’ & ‘date’.
now, make a route named as ‘exceldata’...
Route::get('exceldata', [UsersController::class, 'exportUsersData']); |
now add these two lines inside config/app.php
'providers' => Maatwebsite\Excel\ExcelServiceProvider::class, ],
'aliases' => [ 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ], |
more info: https://www.linkedin.com/pulse/how-export-data-excel-file-laravel-avaneesh-verma