Published on June 18, 2025

Programming is hard - or so you might see it when you are trying to learn it - but if you give up, you will not make great things. ALX, a coding platform training programmers in Python and other languages has this as its mantra, "Do hard things!" Taylor Otwel, Laravel's creator, though, has a different meaning when he says, "do the hard things first". If Laravel had Zen statements like Python does, this would feature first in the list - given that it comes from its creator himself. Well, what does it mean, though. It means that if you want to succeed at creating a big code project, build the hard parts of it first and finish with the easy ones. I would say this is open for discussion. Speaking of hard things, figure out what the below controller does:

Full Code Snippet

<?php

namespace App\Exports;

use App\Models\CqiProject;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Illuminate\Support\Facades\DB;

class CqiProjectReportExport implements FromCollection, WithHeadings, WithMapping
{
    protected $filters;

    public function __construct(array $filters = [])
    {
        $this->filters = $filters;
    }

    /**
     * Fetch the data for the report.
     *
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        $query = CqiProject::query()
            ->select(
                'cqi_projects.*',
                DB::raw('COUNT(audits.id) as audit_count'),
                DB::raw('AVG(audits.score) as average_audit_score')
            )
            ->leftJoin('audits', 'cqi_projects.id', '=', 'audits.cqi_project_id')
            ->groupBy('cqi_projects.id');

        // Apply filters if provided
        if (!empty($this->filters['status'])) {
            $query->where('cqi_projects.status', $this->filters['status']);
        }

        if (!empty($this->filters['methodology'])) {
            $query->where('cqi_projects.methodology', $this->filters['methodology']);
        }

        if (!empty($this->filters['progress_min'])) {
            $query->where('cqi_projects.initial_progress', '>=', $this->filters['progress_min']);
        }

        return $query->get();
    }

    /**
     * Define the headings for the CSV.
     *
     * @return array
     */
    public function headings(): array
    {
        return [
            'Project Name',
            'Status',
            'Methodology',
            'Progress (%)',
            'Audit Count',
            'Average Audit Score',
            'Created At',
        ];
    }

    /**
     * Map each row of data to the CSV format.
     *
     * @param  mixed  $project
     * @return array
     */
    public function map($project): array
    {
        return [
            $project->project_name,
            $project->status,
            $project->methodology,
            $project->initial_progress,
            $project->audit_count,
            number_format($project->average_audit_score, 2),
            $project->created_at->format('Y-m-d'),
        ];
    }
}

Love this nugget?

Support my work to get exclusive premium nuggets and early access via a personal activation link.

Support Now & Unlock Exclusive Content