Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Parent class
- class MultipleUsersExport implements WithMultipleSheets
- {
- use Exportable;
- public function sheets(): array
- {
- $sheets = [];
- $query = DB::table('users')
- ->select('id', 'name')
- ->orderBy('updated_at')
- ->get();
- foreach($query as $user) {
- $sheets[] = new ActiveUsersExport($user->name, $user->id);
- }
- return $sheets;
- }
- public function registerEvents(): array
- {
- return [
- BeforeWriting::class => function(BeforeWriting $event) {
- $event->writer->setActiveSheetIndexByName('3');
- },
- ];
- }
- }
- //Child class
- class ActiveUsersExport implements`
- ` FromQuery, ShouldAutoSize, WithMapping, WithHeadings, WithEvents, WithCharts, WithTitle
- {
- ` use Exportable;`
- public $name;
- public $id;
- public function __construct($name, $id) {
- $this->name = $name;
- $this->id = $id;
- }
- public function query()
- {
- $query = DB::table('users')
- ->select('users.*')
- ->orderBy('updated_at');
- return $query;
- }
- public function headings(): array
- {
- return [
- 'ID',
- 'Name',
- 'Email',
- ];
- }
- public function charts()
- {
- $label = [new DataSeriesValues('String', $this->id.'!$B$1', null, 1), ];
- $categories = [new DataSeriesValues('String', $this->id.'!$B$2:$B$8', null, 2)];
- $values = [new DataSeriesValues('Number', $this->id.'!$A$2:$A8', null, 2)];
- $series = new DataSeries(DataSeries::TYPE_BARCHART_3D, NULL,
- range(0, \count($values) - 1), $label, $categories, $values);
- $plot = new PlotArea(NULL, [$series]);
- $legend = new Legend(Legend::POSITION_RIGHT, NULL, false);
- $chart = new Chart('Overview X', new Title('Overview'), $legend, $plot,true,'gap',NULL,NULL);
- $chart->setTopLeftPosition('E1');
- $chart->setBottomRightPosition('K20');
- return $chart;
- }
- public function registerEvents(): array {
- return [
- AfterSheet::class => function(AfterSheet $event) {
- $chart = $this->charts();
- $event->sheet->getDelegate()->addChart($chart);
- }
- ];
- }
- public function map($user): array
- {
- return [
- [$user->id,
- $user->name,
- $user->email,],
- ];
- }
- public function title(): string
- {
- return $this->id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment