In Laravel 5.8, I am trying to export to Excel using Maatwebsite:
<?php
namespace App\Exports;
use App\User;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\BeforeWriting;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\FromView;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithMapping;
use \Maatwebsite\Excel\Sheet;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Illuminate\Support\Facades\DB;
use App\Models\Appraisal\AppraisalGoal;
use App\Models\Hr\HrEmployee;
use App\Models\Hr\HrGradeLevel;
use App\Models\Hr\HrDepartment;
use App\Models\Appraisal\AppraisalIdentity;
use Auth;
class EmployeesGoalPublishedExport implements FromCollection, ShouldAutoSize, WithHeadings
{
private $headings = [
'Staff ID',
'Name',
'Gender',
'Official Email',
'Department',
'HOD',
'HRBP',
'Line Manager',
'Goal Status'
];
public function collection()
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$userId = Auth::user()->id;
$employeecode = Auth::user()->employee_code;
$identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->pluck('id');
$publishedgoals = AppraisalGoal::select('employee_code')->where('is_published', 1)->where('appraisal_identity_id', $identities)->where('company_id', $userCompany)->groupBy('employee_code')->get();
$published_goals = DB::table('hr_employees AS e')
->join('hr_employees AS em','em.employee_code','=','e.line_manager_id')
->join('hr_departments AS d','e.department_id','=','d.id')
->join('hr_employees AS eh','eh.employee_code','=','d.dept_head')
->join('hr_employees AS eb','eb.employee_code','=','d.hr_business_partner_id')
->join('appraisal_goals AS ag','ag.company_id','=','e.company_id')
->select(
'e.employee_code as staff_id',
DB::raw('CONCAT(e.first_name, " ", e.last_name) AS full_name'),
DB::raw('IF(e.gender_code = 0, "Female", "Male") AS gender'),
'e.email as official_email',
'd.dept_name',
DB::raw('CONCAT(eh.first_name, " ", eh.last_name) AS hod_name'),
DB::raw('CONCAT(eb.first_name, " ", eb.last_name) AS hrbp_name'),
DB::raw('CONCAT(em.first_name, " ", em.last_name) AS line_manager'),
DB::raw('(CASE WHEN ag.is_approved = 3 THEN "Approved" WHEN ag.is_approved = 2 THEN "Not Approved" WHEN ag.is_approved = 1 THEN "Awaiting Approval" ELSE "Draft" END) AS goal_status')
)
->whereIn('e.employee_code', $publishedgoals)
->where('e.company_id', $userCompany)
->where('e.hr_status', 0)
->distinct()
->get();
$published_goals = $published_goals->unique('staff_id');
$published_goals = array_slice($published_goals->values()->all(), 0, 5, true);
return $published_goals;
}
public function headings() : array
{
return $this->headings; //english
}
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:I1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
},
];
}
}
Export Controller
<?php
namespace App\Http\Controllers\Report;
use App\Http\Controllers\Controller;
use App\Exports\EmployeesGoalPublishedExport;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
class ExportController extends Controller
{
public function submitted_goals(){
return Excel::download(new EmployeesGoalPublishedExport, 'submitted_goals.xlsx');
}
}
But I got this error:
production.ERROR: Call to a member function all() on array {"userId":1,"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to a member function all() on array at C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Sheet.php:399)
How do I resolve it?