如何解决Class App\Exports\FormExport在将数据从Laravel导出到Excel时包含1个抽



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


<?php
namespace AppExports;
use AppCSA_Form as CSA_Form;
use IlluminateContractsViewView;
use MaatwebsiteExcelConcernsFromView;
use MaatwebsiteExcelConcernsShouldAutoSize;
use MaatwebsiteExcelConcernsWithEvents;
use MaatwebsiteExcelEventsAfterSheet;
Use MaatwebsiteExcelSheet;

class FormExport implements FromView, ShouldAutoSize, WithEvents
{
/**
* @return IlluminateSupportCollection
*/
//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 AppHttpControllers;
use IlluminateHttpRequest;
use MaatwebsiteExcelExcel;
use AppExportsFormExport;
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 IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;
use SpatieActivitylogContractsActivity;
use SpatieActivitylogTraitsLogsActivity;
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('AppYearly_Student', 'yearly_student_id');
}
// Has one relationships
public function english_test(){
return $this->hasOne('AppEnglish_Test', 'csa_form_id');
}
public function academic_info(){
return $this->hasOne('AppAcademic_Info', 'csa_form_id');
}
public function passport(){
return $this->hasOne('AppPassport', 'csa_form_id');
}
public function emergency(){
return $this->hasOne('AppEmergency', 'csa_form_id');
}
public function condition(){
return $this->hasOne('AppCondition', 'csa_form_id');
}
// Has many relationships
public function achievements(){
return $this->hasMany('AppAchievement', 'csa_form_id');
}

public function choices(){
return $this->hasMany('AppChoice', 'csa_form_id');
}
public function personal_info(){
return $this->hasMany('AppPersonal_Info', 'csa_form_id');
}
}

English_Test.php型号


namespace App;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;
use SpatieActivitylogContractsActivity;
use SpatieActivitylogTraitsLogsActivity;
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('AppCSA_Form', 'csa_form_id');
}
}

如何解决这个问题?类App\Exports\FormExport包含1个抽象方法,因此必须声明为抽象方法或实现其余方法(Maatwebsite\Excel\Construs\WithEvents::registerEvents(。

您需要在代码中添加样式函数

https://docs.laravel-excel.com/3.1/exports/column-formatting.html命名空间应用程序\导出;

使用Maatewebsite\Excel\Consults\WithStyles;使用PhpOffice\PhpSpreadsheet\Workheet\Worksheet;

类InvoicesExport实现WithStyles{公共函数样式(工作表$sheet({返回[//将第一行设置为粗体文本样式。1=>['font'=>['bold'=<true]],

// Styling a specific cell by coordinate.
'B2' => ['font' => ['italic' => true]],
// Styling an entire column.
'C'  => ['font' => ['size' => 16]],
];
}

}

您的类实现

WithEvents

如果您没有任何理由,您应该在类定义中忽略这一点。否则,您可能需要实现registerEvents((。看这里:laravel卓越

最新更新