jeudi 14 février 2019

Count by dynamic field value Laravel

I am trying to generate a report based on the value of a field report_template_type, like this.

enter image description here

How can I achieve this in Laravel? Tried the following but not sure how to pass the dynamically generated select items to get method.

$report = Report::all();
$collection = collect($report);
$templates = $collection->sortBy('report_template_type')
   ->groupBy('report_template_type')
   ->map(function($values) {
      return $values->count();
});

$get = ['user_id'];
foreach ($templates as $key => $value) {
    array_push($get, 'SUM(report_template_type = \''.$key.'\') as \''.$key.'\'');
}

return Report::groupBy('user_id')->with(['user'])->get(); // dynamic db::raw inside get()



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire