Php Laravel Excel父/子类

Php Laravel Excel父/子类,php,laravel,Php,Laravel,我正在为我的Laravel应用程序使用以下名为Laravel Excel的软件包。下面提供了链接 我想做的是创建一个父类,它将生成我的所有报告,我可以拥有子类,在子类中定义列标题和创建Excel文档所需的数据。我环顾四周,看看是否有人做到了这一点。我认为情况并非如此。到目前为止我有什么建议吗 <?php namespace App\Reports; use Excel; class Report { protected $columnHeadings = [];

我正在为我的Laravel应用程序使用以下名为Laravel Excel的软件包。下面提供了链接

我想做的是创建一个父类,它将生成我的所有报告,我可以拥有子类,在子类中定义列标题和创建Excel文档所需的数据。我环顾四周,看看是否有人做到了这一点。我认为情况并非如此。到目前为止我有什么建议吗

<?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);
    }
}

您试图实现的目标被称为。在这里,你可以有一个基类,通常它并没有被内化,它就像一个解析器,所以大多数时候你会想要一个抽象类作为基类

在该基类中,您可以在案例
filename
columns
中定义一些属性,这些属性将从子类重写。您可能需要的类结构:

您的案例中的代码:

<?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);
            });
        });
    }
}

<?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);
            });
        });
    }
}
<?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();
      }
 
}