How do I add exporters to my custom report?

To add an exporter to your report you need to ensure the report uses the Aero\Admin\Reports\Traits\CanExport trait and that the exporter class is in the reports protected static $exports array.

By default Aero provides a CSV, XML, and JSON exporter.

<?php

namespace Acme\MyModule\Reports;

use Aero\Admin\Reports\Exporters\CSVExporter;
use Aero\Admin\Reports\Exporters\JSONExporter;
use Aero\Admin\Reports\Exporters\XMLExporter;
use Aero\Admin\Reports\Report;
use Aero\Admin\Reports\Traits\CanExport;
use Aero\Cart\Models\Order;
use Illuminate\Database\Eloquent\Builder;

class OrdersReport extends Report
{
   use CanExport;

   protected function newQuery(): Builder
   {
       return Order::with([
           'status', 'items', 'currency', 'discounts', 'payments.method',
       ]);
   }

   protected static $exports = [
       CSVExporter::class,
       XMLExporter::class,
       JSONExporter::class,
   ];
}

 

Creating a Report Exporter

You need to create a class for your exporter that extends Aero\Admin\Reports\ReportExporter and implements the required public handle function.

Handle Parameters

Type Description
Collection The first parameter is a collection of the exportable tables columns.
Builder The second parameter is the query that should be used to get the data for the export.
String The third parameter is the key of the table being exported.
<?php

namespace Acme\MyModule\Reports\Exporters;

use Aero\Admin\Reports\ReportExporter;
use Carbon\Carbon;

class CustomExporter extends ReportExporter
{
   public function handle($columns, $rows, $key)
   {
       $file = $key.'-'.Carbon::now().'.csv';

       return response()->streamDownload(function () use ($columns, $rows) {
           $file = fopen('php://output', 'wb');

           fputcsv($file, $columns->map->header()->toArray());

           $rows->chunk(100, function ($chunk) use ($file, $columns) {
               foreach ($chunk as $row) {
                   $rowData = [];

                   foreach ($columns as $column) {
                       $rowData[] = $column->exportContent($row);
                   }

                   fputcsv($file, $rowData);
               }
           });

           fclose($file);
       }, $file,
           ReportExporter::getHttpHeaders('text/csv', $file)
       );
   }
}

Now you need to add your newly created exporter to your report as shown at the start of this article.

Articles in this section

Was this article helpful?
0 out of 0 found this helpful