-1

I have a table in my blade page,

<table>
  <thead>
    <tr>
      <th>Color</th>
      <th>Total</th>
    </tr>
  </thead>
  <tbody>
    @if ($counts)
      @foreach ($counts as $item)
        <tr>
          <td>{{ $item->color }}</td>
          <td>{{ $item->total }}</td>
        </tr>
      @endforeach
    @endif
  </tbody>
</table>

I want to show the sum total of the column Total. I googled for the solution but all suggest to use server side sum function. Is it possible to do it in blade page?

public function get_color_distribution(Request $request)
{
    $date_from = Carbon::parse($request->input('n_from_date'))->startOfDay();
    $date_to = Carbon::parse($request->input('n_to_date'))->endOfDay();
    
    $counts = Production::join('finishes', 'finishes.id', '=', 'productions.color')
        ->select(
            DB::raw('count(*) as total'),
            'finishes.description as color'
        )
        ->whereDate('productions.created_at', '>=', $date_from)
        ->whereDate('productions.created_at', '<=', $date_to)
        ->groupBy('finishes.description')
        ->get();
    
    return view('admin.reports.r_colors_gen', compact(
        'counts',
        'date_from',
        'date_to'
    ));
}

If I need to do it in server side how to get the total sum of DB::raw('count(*) as total') value?

2
  • Do you mean that you want to do this client-side in JavaScript, rather than server-side in PHP? Commented Sep 5 at 11:08
  • 4
    You can just calculate it on the collection before sending to Blade, e.g.: $totalSum = $counts->sum('total'); return view('admin.reports.r_colors_gen', compact('counts', 'date_from', 'date_to', 'totalSum')); Commented Sep 5 at 11:12

1 Answer 1

1

You already have an instance of a Collection stored in your $counts variable.

You can use its sum method just like in the example.

<table>
  <thead>
    <tr>
      <th>Color</th>
      <th>Total</th>
    </tr>
  </thead>
  <tbody>
    @if ($counts)
      @foreach ($counts as $item)
        <tr>
          <td>{{ $item->color }}</td>
          <td>{{ $item->total }}</td>
        </tr>
      @endforeach
      <tr>
        <td>-</td>
        <td>{{ $counts->sum('total') }}</td>
      </tr>
    @endif
  </tbody>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

In case you didn't do so, please seek pre-existing pages to nominate as dupe targets before considering posting a new answer.
I'm willing to go through laravel's documentation to find the example I'm thinking about that solves the question. I am not willing to go through stackoverflow questions to hunt for dupes. All answers given are voluntary.
Your accumulated reputation has unlocked the privilege of being able to vote to close questions which have been resolved elsewhere. Aligning your contributions with the content curation goals of this platform is the expected behavior. It would have taken you less time to find a duplicate versus posting a new answer. See ...have already been asked and answered many times before.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.