How to Generate or Export Excel in Laravel 8
Generate Excel di Laravel - The generate or export excel feature may be really needed in a system, especially if the system is an information system or a back office system for example. Now to generate or export excel in the laravel framework is very easy. We can use the package from laravel-excel.com. In this article, we will both learn how to create an excel generate or export feature in laravel. And in this experiment, we will use the source code of the company profile website that I shared before.
Read: Download Source Code Website Company Profile (Laravel 8 & Bootstrap)
Alright, let's start with the following steps:
1. Open the company profile project folder
2. Open a terminal and go to the project directory
3. Run the command to install the laravel excel package from the maatwebsite in the project we are using.
composer require maatwebsite/excel
4. Because in this experiment, we will try to add the export excel feature in the team menu so we need to create a TeamsExport file. To create the Export file, you can use the command below.
php artisan make:export TeamsExport --model=Team
With the command above, it will create us the TeamsExport file in the new export folder.
5. In this experiment we are using a project that uses the laravel framework version 8, so we need to adjust the code created in step no. 4.
Go to the App\Exports\TeamExports.php file then make the changes as below.
use App\Team;
to
use App\Models\Team;
6. Open the TeamController.php file, then make the changes as below.
Add code
use App\Exports\TeamsExport;
use Maatwebsite\Excel\Facades\Excel;
and
public function export()
{
return Excel::download(new TeamsExport, 'team.xlsx');
}
7. Then in the routes>web.php file add the code as below
use App\Exports\TeamExport;
and
Route::get('teams/export/', [TeamController::class, 'export'])->name('export');
8. Then lastly, we need to add an export button in the view. Open the index.blade.php file in resources>views>admin>team>index.blade.php
Then add the code below, just below the "create team" button.
<a href="{{ route('export') }}" class="btn btn-primary">Export Excel</a>
Finished. Now please try running php artisan serve and go to admin>team menu. There is already an export excel button that can be directly used to generate or export excel.
- Cara Mengatasi Error XAMPP: MySQL shutdown unexpectedly 23 Oktober 2021 66463 views
- Laravel 8: REST API Authentication dengan Sanctum 17 September 2021 31937 views
- Tutorial CRUD (Create, Read, Update & Delete) Codeigniter 4 dengan Bootstrap 14 Oktober 2021 30569 views
- Membuat REST API CRUD di Laravel 8 dengan Sanctum 18 September 2021 28370 views
- Contoh Cara Menggunakan Sweet Alert di Laravel 8 27 Agustus 2021 27621 views