array 必须与 Maatwebsite\Excel\Concerns\WithMapping::map($row) in - Laravel 兼容

array must be compatible with Maatwebsite\Excel\Concerns\WithMapping::map($row) in - Laravel

我正在使用 Laravel-5.8 和 Maatwebsite-3.1 导出到 excel。

<?php

namespace App\Exports;

use App\User;
use Auth;

class StudentExport implements FromCollection, ShouldAutoSize, WithHeadings, WithMappings, WithCustomStartCell
{

    private $headings = [
        'Student ID', 
        'Name',
        'Class',
        'Status',
        'Teacher'
    ];

    public function collection()
    {
        $current_terms = DB::table('appraisal_identity')->select('term_name')->where('company_id', $userCompany)->where('is_current', 1)->first()->term_name;
        $publishedgoals = AppraisalGoal::select('employee_code')->where('is_published', 1)->where('company_id', $userCompany)->groupBy('employee_code')->get();  


        $published_goals = DB::table('hr_students AS e')
                    ->join('hr_employees AS em','em.id','=','e.teacher_id')
                     ->select(
                        'e.student_id',
                        DB::raw('CONCAT(e.first_name, " ", e.last_name) AS full_name'),
                        'e.student_class,
                        DB::raw('(CASE WHEN e.is_status = 3 THEN "Excellent" WHEN e.is_status = 2 THEN "Good" WHEN e.is_status = 1 THEN "Average" ELSE "Pass" END) AS student_status') 
                        DB::raw('CONCAT(em.first_name, " ", em.last_name) AS teacher_name')
                        
                 )
                    ->whereIn('e.student_id', $publishedgoals)
                ->distinct()
                ->get();

            $published_goals = $published_goals->unique('student_id');
       
           return collect($published_goals, $current_terms);
    } 

public function map($published_goals, $current_terms): array
{
    return [
       $published_goals->student_id,
       $published_goals->full_name,
       $published_goals->student_class,
       $published_goals->student_status,
       $published_goals->teacher_name,
       $current_terms->term_name,
   ];
 }  

public function startCell(): string
{
    return 'A4';
}    


public function headings() : array
{

    return $this->headings;
}    

public function registerEvents() : array
{
    return [
        AfterSheet::class    => function(AfterSheet $event) {

            $event->sheet->setCellValue('A2', 'Current Term:'); 
            $event->sheet->getDelegate()->setCellValue('B2', $current_terms);


            $cellRange = 'A4:E4'; // All headers
            $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
            $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->getColor()
                   ->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);
            $event->sheet->getDelegate()->getStyle($cellRange)->getFill()
                   ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
                   ->getStartColor()->setARGB('FF17a2b8');
            $event->sheet->setAutoFilter($cellRange);
        },
    ];
  }   

}

这是我的预期输出:

我写了上面的代码得到了这个结果:

我想让 B2 有这个变量的输出:$current_terms

我收到这个错误:

ERROR: Declaration of App\Exports\HrEmployeeGoalExport::map($published_goals, $current_terms): array must be compatible with Maatwebsite\Excel\Concerns\WithMapping::map($row)

我该如何解决这个问题?

谢谢

您正在实施 Maatwebsite\Excel 包中的一些契约,其中一个契约指定您必须实施一个 map 方法,该方法以 $row 作为参数; Maatwebsite\Excel\Concerns\WithMapping::map($row).

在你的class中你有HrEmployeeGoalExport::map($published_goals, $current_terms): array

要使其正常工作,您必须将映射函数更改为 HrEmployeeGoalExport::map($row),然后使用 $row 进行任何映射。'

因此,您需要更改收到的参数并删除您指定的 return 类型。