如何解决从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');
}
}
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。