@brandy
To export data tables in multiple tabs in Laravel, you can use the Laravel Excel package. Here's a step-by-step guide to achieve this:
- Install Laravel Excel package by running the following composer command:
1
|
composer require maatwebsite/excel
|
- After installing the package, publish the configuration file by running the following command:
1
|
php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider"
|
- Create a new export class for each data table you want to export. You can do this by running the following command:
1
|
php artisan make:export DataTable1Export --model=DataTable1Model
|
- In the export class, define the sheets method to export data in multiple tabs. Here's an example of how to export data in multiple tabs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
use MaatwebsiteExcelConcernsExportable;
use MaatwebsiteExcelConcernsWithMultipleSheets;
use MaatwebsiteExcelSheet;
class ExportMultipleTabs implements WithMultipleSheets
{
use Exportable;
public function sheets(): array
{
$sheets = [];
$sheets[] = new DataTable1Export(); // Export data table 1 in the first tab
$sheets[] = new DataTable2Export(); // Export data table 2 in the second tab
return $sheets;
}
}
|
- To export the data tables in multiple tabs, you can use the following code in your controller or route:
1
2
3
4
5
6
7
|
use AppExportsExportMultipleTabs;
use MaatwebsiteExcelFacadesExcel;
public function exportMultipleTabs()
{
return Excel::download(new ExportMultipleTabs, 'multiple_tabs.xlsx');
}
|
- Finally, create a route to trigger the export function:
1
|
Route::get('/export-multiple-tabs', 'YourController@exportMultipleTabs');
|
After following these steps, your data tables will be exported in multiple tabs in an Excel file when you visit the specified route.