Laravel - keeping a running tally of a column through a transformer

- 1 answer


So I have a Contribution model. I have a controller that pulls in all the contributions and sends them to a transformer like so:

My Controller:

public function showContributions (Request $request, $memberId)
    $perPage = $request->input('per_page', 15);
    $contribution = parent::getRepo('Contribution');

    $contributions = Cache::tags(['contributions'])->remember("contributions.$memberId.2", 60, function() use ($perPage, $memberId, $contribution){
        return $contribution->where('vip_id', $memberId)->where('fund_id', 2)->paginate($perPage);

    $transformedData = $this->fractal->paginatedCollection($contributions, new ContributionTransformer(), 'contributions');
    return $this->sendResponse($transformedData['contributions'], $transformedData['meta']);

My transformer:

    public function transform(Contribution $contribution)
        setlocale(LC_MONETARY, 'en_US.UTF-8'); // Set so that money_format uses the dollar sign instead of USD. Consider moving to bootstrap
        $report = $contribution->report;
        $employer = $report->employer;
        $employerHours = $contribution->employerHours;

        $contributionLocal = $contribution->local->local_code ?? '';
        $employerLocal = $employerHours->local->local_code ?? '';

        $reciprocalLocal = $contributionLocal === $employerLocal ? '0000' : $employerLocal;

        $response = [
            'id' => $contribution->member_hours_id,
            'report_number' => $contribution->report_number_id,
            'employer_code' => $employer->employer_code,
            'employer_name' => $employer->employer_name,
            'worked_date' => $report->ending_worked_date,
            'received_date' => $report->receipt_date,
            'report_local' =>  $contributionLocal,
            'reciprocal_local' => $reciprocalLocal,
            'unit_type' =>  $contribution->unitType->code_description,
            'units_worked' => $contribution->units_worked,
            'credited_units' => $contribution->units_credited,
            'rate' => $contribution->unit_multiplier,
            'reciprocal_rate' => $employerHours->reciprocal_multiplier,
            'calculated_amount' => money_format('%.2n', $contribution->calculated_amount),
            'received_amount' => money_format('%.2n', $contribution->actual_amount),
            'owed_amount' => money_format('%.2n', $contribution->owed_amount),

        return $response;

One of the fields in the contributions table is sub_hours. What they want me to do is keep a running tally of said field. In each subsequent row return that tally as hours_to_date. So in first row sub_hours is 32 and in the second row it is 60. In the first row hours_to_date will be 32 but in the second row it will be 92 and the third row it will be 92 + sub_hours of row 3 etc. I can't seem to figure out how I should keep track of this running tally and allow the transformer access to it. Any help would be appreciated.




Can you create a property on the transformer class? I haven't used transformers but something like

class ContributionTransformer{
    private $tally;
    function __construct(){
        $this->tally = 0;
    public function transform(Contribution $contribution){
            $this->tally += $contribution->sub_hours;