Laravel Excel Parent/Child Classes

I am using the following package called Laravel-Excel for my Laravel application. Link provided below.

What I would like to be able to do is create a parent class that will generate all my reports and I can have child classes where I define column headings and the data needed to create the Excel document. I looked around to see if anyone has accomplished this. I have not found this to be the case. Any suggestions on what I have so far?

<?php

namespace App\Reports;

use Excel;

class Report 
{
    protected $columnHeadings = [];

    protected $dataSet;

    public function generate()
    {
        Excel::create($filename, function ($excel) {
            $excel->sheet($sheetname, function ($sheet) {
                $sheet->appendRow($this->columnHeadings, null, 'A1', false, false);
                $dataSet->each(function($data) use ($sheet) {

                });
            });
        })->download($type);
    }
}

http://www.maatwebsite.nl/laravel-excel/docs

1 answer

  • answered 2018-01-11 20:48 Leo instanceof Kelmendi

    What you are trying to achieve it's called template pattern design. Where you can have a base class that often its not incialized it kind of serves as a resolver, so most of the time you would want an abstract class as a base class.

    In that base class you can define some properties in your case filename and columns, which will be overridden from the child class. The class structure you may want its:

    Base Abstract ReportGenerator 
    
     ->Child MonthlyReport Class 
    
     ->Child YearlyReport Class
    

    Code in your case:

    <?php
    
    namespace App\Reports;
    
    use Excel;
    
    abstract class ReportGenerator
    {
        protected $columnHeadings = [];
    
        protected $dataSet;
    
        protected $fileName;
    
        public function generate()
        {
            return Excel::create($this->filename, function ($excel) {
                $excel->sheet($sheetname, function ($sheet)use ($headings = $this->columnHeadings) {
                    $sheet->appendRow($headings, null, 'A1', false, false);
                    $dataSet->each(function($data) use ($sheet) {
    
                    })->download($type);
                });
            });
        }
    }
    

    Then in your child class:

    <?php
    
    namespace App\Reports;
    
    use App\Reports\ReportGenerator;
    
    
    class MonthlyReport extends ReportGenerator {
    
            //set the properties for this specific class so they can override the base properties whenever this class its init.
    
          protected $columnHeadings = [];
    
          protected $dataSet;
    
          protected $fileName ='Monthly Report';
    
          public function __construct(SomeModel $model){
    
               //set the required headings for this specific report
               $this->columnHeadings = ['some headings']; 
               $this->data = $model;
    
          }
    
          public function generateMonthlyReport(){
    
             return $this->generate();
          }
    
    }
    

    So whenever you want to generate a monthly report you might do:

    MonthlyReport->generate($someModel); and you can create other classes to generate different reports but extending abstract claass!