rudi97277 / export-db
Export data to XLSX or CSV formats from database queries or functions
Requires
- illuminate/database: ^10.48
- illuminate/http: ^10.48
- maatwebsite/excel: ^3.1
README
install this package using
composer require rudi97277/export-db
How to Use This Package
-
Run the Migration
- Execute the migration to create the necessary database structure.
-
Table Creation
- A new table named
export_configs
will be generated.
- A new table named
-
Insert Data
- Insert new data into the
export_configs
table to create a new report.
- Insert new data into the
-
Create a New Route
- Define a new route that creates an instance of the
Rudi9277/ExportDb/GenerateReport
class and calls thegenerate
function. Pass therequest
as a parameter.
Example route definition:
Route::get('export', function () { return Rudi9277\ExportDb\GenerateReport::generate(request()); });
- Define a new route that creates an instance of the
-
Run Symlink
- If you never run a symlink command before, please run it
php artisan storage:link
- If you never run a symlink command before, please run it
Note:
- Available
export_type
arexlsx
andcsv
. - You can create a new module in the database.
Table columns:
-
module: The name of the module that will be exported.
-
title: The title of the sheet in the Excel that will be generated.
-
query: The query that will be used to generate the Excel.
-
formatter: JSON object that will help format the data the way you want.
Example:
[ { "name": "Product", "value": "--- {product_name} ----" } ]
Note:
- The
"name"
key is the header in the Excel that will be generated. {product_name}
is the column name in the SQL query result.
- The
-
validator: JSON object that will help to validate the required data for the query. The validator is from Laravel Validator.
Example:
{ "name": "required|string" }
-
default: A default JSON object that sets what the default value of the validator in No. 5 is.
Example:
{ "name": null }
-
To style the data, you can use ExportDTO to pass callable function to the generator like this example
Route::get('export', function () { $style = function (Worksheet $sheet) { return [ 'A' => [ 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, ], ], ]; }; $reg = function () { return [ AfterSheet::class => function ($event) { $sheet = $event->sheet->getDelegate(); $sheet->getStyle('B:B')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); }, ]; }; $dto = new ExportDTO($func, $style); return Rudi97277\ExportDb\GenerateReport::generate(request(),$dto); });
please check Laravel Excel how to use the styles