从Laravel导出数据到Excel时,如何解决App \ Exports \ FormExport类包含1种抽象方法?

问题描述

我想在Laravel中导出表格以表现出色。但是我有一些错误:App \ Exports \ FormExport类包含1个抽象方法,因此必须声明为抽象或实现其余方法(Maatwebsite \ Excel \ Concerns \ WithEvents :: registerEvents)。这是我的导出代码(FormExport.php):


<?php

namespace App\Exports;

use App\CSA_Form as CSA_Form;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
Use \Maatwebsite\Excel\Sheet;


class FormExport implements FromView,ShouldAutoSize,WithEvents
{
    /**
    * @return \Illuminate\Support\Collection
    */

    //use Exportable;
    public function view(): View
    {
    
            $csa_form = CSA_Form::with(['english_test']);
            return view('excel.csaformtable1',[
                'csa_form' => $csa_form->get()
            ]);
    }
}

这是导出控制器(ConvertExcelController.php)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Maatwebsite\Excel\Excel;
use App\Exports\FormExport;

class ConvertExcelController extends Controller
{
    private $excel;

    public function __construct(Excel $excel)
    {
        $this->excel = $excel;
    }

    public function export_questions(){
  
        return \Excel::download(new FormExport(),"csaformtable1.xlsx");
    }
}

这是我的模型(CSA_Form.php),它与English_Test.php相关 CSA_Form.php模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\Traits\LogsActivity;

class CSA_Form extends Model
{
    use SoftDeletes;
    use LogsActivity;
    
    // not to use the convention table 'CSA_Forms'
    protected $table = 'csa_forms';

    protected $guarded = ['id','yearly_student_id','is_submitted'];

    protected $attributes = ['is_submitted' => false];
    
    // Custom timestamps field name
    const CREATED_AT = 'latest_created_at';
    const UPDATED_AT = 'latest_updated_at';

    // Custom soft delete field name
    const DELETED_AT = 'latest_deleted_at';

    // Log changes only on stated attributes
    protected static $logAttributes = ['is_submitted'];

    // Customize log name
    protected static $logName = 'csa_form_log';

    // Log only changed attributes
    protected static $logOnlyDirty = true;

    
    // function for custom defining custom attributes
    public function tapActivity(Activity $activity,string $eventName)
    {
        if(strcmp($eventName,'created') == 0 || strcmp($eventName,'deleted') == 0){
            $activity->properties = null;
        }
    }

    // Relationships
    // Inverse has one relationship
    public function yearly_student(){
        return $this->belongsTo('App\Yearly_Student','yearly_student_id');
    }

    // Has one relationships
    public function english_test(){
        return $this->hasOne('App\English_Test','csa_form_id');
    }

    public function academic_info(){
        return $this->hasOne('App\Academic_Info','csa_form_id');
    }

    public function passport(){
        return $this->hasOne('App\Passport','csa_form_id');
    }

    public function emergency(){
        return $this->hasOne('App\Emergency','csa_form_id');
    }

    public function condition(){
        return $this->hasOne('App\Condition','csa_form_id');
    }

    // Has many relationships
    public function achievements(){
        return $this->hasMany('App\Achievement','csa_form_id');
    }
    
    public function choices(){
        return $this->hasMany('App\Choice','csa_form_id');
    }
    public function personal_info(){
        return $this->hasMany('App\Personal_Info','csa_form_id');
    }
}

English_Test.php模型


namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\Traits\LogsActivity;

class English_Test extends Model
{
    use SoftDeletes;
    use LogsActivity;
    
    // custom primary key,not auto-incrementing,protected $primaryKey = 'csa_form_id';
    
    public $incrementing = false;

    // not to use the convention table
    protected $table = 'english_tests';

    protected $guarded = ['csa_form_id','proof_path'];

    // Custom timestamps field name
    const CREATED_AT = 'latest_created_at';
    const UPDATED_AT = 'latest_updated_at';

    // Custom soft delete field name
    const DELETED_AT = 'latest_deleted_at';

    
    // Log changes only on stated attributes
    protected static $logAttributes = ['test_type','score','test_date','proof_path'];
    
    // Customize log name
    protected static $logName = 'english_test_log';

    // Log only changed attributes
    protected static $logOnlyDirty = true;

    // function for custom defining custom attributes
    public function tapActivity(Activity $activity,'deleted') == 0){
            $activity->properties = null;
        }
    }
    
    // Relationships
    // Inverse has one relationship
    public function csa_form(){
        return $this->belongsTo('App\CSA_Form','csa_form_id');
    }
}


如何解决这个问题? Class App\Exports\FormExport contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Maatwebsite\Excel\Concerns\WithEvents::registerEvents).

解决方法

您的课堂工具

WithEvents

如果没有理由,则应将其保留在类定义中。否则,您可能要实现registerEvents()。看这里:laravel excel

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...