【问题标题】:Count Data In Laravel在 Laravel 中计算数据
【发布时间】:2019-02-19 07:50:29
【问题描述】:

我想根据 Laravel 5.7 中的状态统计数据。我有六个状态(已分配、正在路上、已开始、取消工作人员、取消管理员和已关闭)。我必须在 foreach 数据中对此进行查询。

这是我的代码:

  $data['list_detail'] = [];

  foreach ($listFa as $value) {
    $type = $value->type;
    $descr = $value->descr;

    $c_ass_detail = transaction::where('type', $value->type)
                                ->where('status','like','%Assigned%')
                                ->groupBy('type')
                                ->count();

    $c_otw_detail = transaction::where('type', $value->type)
                                ->where('assign_status','like','%On The Way%')
                                ->groupBy('type')
                                ->count();

    $c_str_detail = transaction::where('type', $value->type)
                                ->where('assign_status','like','%Started%')
                                ->groupBy('type')
                                ->count();

    $c_cbw_detail = transaction::where('type', $value->type)
                                ->where('assign_status','like','%Cancel Worker%')
                                ->groupBy('type')
                                ->count();

    $c_cba_detail = transaction::where('type', $value->type)
                                ->where('assign_status','like','%Cancel Admin%')
                                ->groupBy('type')
                                ->count();

    $c_cls_detail = transaction::where('type', $value->type)
                                ->where('assign_status','like','%Closed%')
                                ->groupBy('type')
                                ->count();

    $total = $c_ass_detail +  $c_otw_detail +  $c_str_detail +  $c_cbw_detail + $c_cba_detail + $c_cls_detail;                         

    array_push($data['list_detail'], array('type'=>$type, 'descr'=>$descr, 'c_ass_detail'=>$c_ass_detail, 'c_otw_detail'=>$c_otw_detail, 'c_str_detail'=>$c_str_detail, 'c_cbw_detail'=>$c_cbw_detail, 'c_cba_detail'=>$c_cba_detail, 'c_cls_detail'=>$c_cls_detail, 'total'=>$total));
  }

这是我的视图代码:

   <tbody>
       @foreach($list_detail_fa as $row)
      <tr class="tr_dashboard">
          <td>{{ $row['fa_type_cd'] }}</td>
          <td>{{ $row['descr'] }}</td>
          <td>{{ $row['c_ass_detail'] }}</td>
          <td>{{ $row['c_otw_detail'] }}</td>
          <td>{{ $row['c_str_detail'] }}</td>
          <td>{{ $row['c_cbw_detail'] }}</td>
          <td>{{ $row['c_cba_detail'] }}</td>
          <td>{{ $row['c_cls_detail'] }}</td>
          <td>{{ $row['total'] }}</td>
      </tr>
       @endforeach                    
   </tbody>

它正在工作,但我认为这种方式不好,因为当我刷新页面时,显示数据的速度很慢。也许是因为许多相同的查询。那么,如果我想只对每个状态使用一个查询来计算数据,那么有什么好方法

【问题讨论】:

  • 我相信,你应该分组assign_status。您可以使用单个查询,例如$c_otw_detail = transaction::where('type', $value-&gt;type)-&gt;groupBy('assign_status')-&gt;count();
  • 我如何输入assign_status value?在这样的数组中assign_status=['status1','status2',etc] ?
  • 为什么要在assign_status 上使用 where 子句?您是否尝试过我上面评论的查询?
  • 因为assign_status有6种类型状态,所以我使用assign_status上的where子句逐一检查类型状态
  • 我已经更新了我的答案,试试吧

标签: php laravel laravel-5 php-7


【解决方案1】:

您需要在查询中使用聚合函数 count 和 group by。假设您的表名是事务。试试这个!

$typeCounts = DB::table('transactions')
              ->select('type', 'assign_status', DB::raw('count(type) as type_count'))
              ->where('type', $value->type)
              ->groupBy('assign_status')
              ->get();

然后在 twig 中你可以像这样访问它:

   <tbody>
      <tr class="tr_dashboard">
         @foreach($typeCounts as $row)
          <td>{{ $row['type_count'] }}</td>
        @endforeach    
      </tr>                  
   </tbody>

【讨论】:

    【解决方案2】:

    你自己把事情复杂化了:

    你只需要:

    $allTypes = array_map(function ($value) {
               return $value->type;
    }, $listFa);
    
     $c_ass_detail = transaction::whereIn('type', $allTypes)
                                ->groupBy('type')
                                ->groupBy('assign_status')
                                ->select('type', 'assign_status', \DB::raw('count(*) as count'))
                                ->get();
    

    然后您的$c_ass_detail 将有一个用于每种类型、分配状态、计数组合的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多