To export data to an Excel file in Laravel, you can use the Laravel Excel package, which provides a simple way to export data to Excel and CSV formats. Follow these steps to export data to an Excel file:
composer require maatwebsite/excel
composer require maatwebsite/excel --with-all-dependencies
'providers' => [
/* * Package Service Providers... */
Maatwebsite\Excel\ExcelServiceProvider::class,
]
add the Facade in config/app.php:
'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
Publish the configuration file to customize settings: open CMD and Enter bellow code.
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
This command will create a excel.php file in the config directory.
php artisan make:export studentExport --model=model_name
generate this file app → Export directory.
Note : Here user is Model name where i want to fetch data and name, email is database column name.
Note : here create a button when user click this button then excel fill auto generate or download.
Route::get('export_student',[studentController::class,'export_student'])->name('export_student');
public function export_student()
{
return Excel::download(new studentExport,'file_name.xlsx');
}
Note : finish, now go to Export button then click on it then display excel file generate.
Remember to replace User with your actual model class and adjust the export logic according to your application's requirements. This example exports name and Email users' data, but you can customize it to export specific data or format the exported data as needed.
I'm a dedicated full-stack developer, entrepreneur, and the proud owner of ocec.org.in , hailing from the vibrant country of India. My passion lies in creating informative tutorials and sharing valuable tips that empower fellow artisans in their journey. With a deep-rooted love for technology, I've been an ardent enthusiast of PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, Codeigniter, and Bootstrap from their earliest days. My philosophy revolves around the values of hard work and unwavering consistency, driving me to continuously explore, create, and share my knowledge with the tech community.
helllo