0
votes

I want to export table in Laravel to excel. But I have some error: Class App\Exports\FormExport contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Maatwebsite\Excel\Concerns\WithEvents::registerEvents). This is my export code(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()
            ]);
    }
}

This is the export controller(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");
    }
}

This is my model(CSA_Form.php) which is connected with English_Test.php CSA_Form.php model

<?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 model


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, string $eventName)
    {
        if(strcmp($eventName, 'created') == 0 || strcmp($eventName, 'deleted') == 0){
            $activity->properties = null;
        }
    }
    
    // Relationships
    // Inverse has one relationship
    public function csa_form(){
        return $this->belongsTo('App\CSA_Form', 'csa_form_id');
    }
}


How to solve this problem? Class App\Exports\FormExport contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Maatwebsite\Excel\Concerns\WithEvents::registerEvents).

1

1 Answers

-1
votes

Your class implements

WithEvents

If you have no reason for that you should leave that out in your class definition. Otherwise you might want to implement registerEvents(). Look here: laravel excel