How to get sum of array in foreach in laravel 6

故事扮演 提交于 2020-12-13 18:58:49

问题


from my controller sum is not working

public function show($id)
{

$workers = DB::table('workers')
->select('wrk_id','f_name','m_name','l_name')
   ;
$attendance = DB::table('attendance')->where('payroll_daily_id', $id)
->select('attendance.*','f_name','l_name','m_name',DB::raw('SUM(reg_hour)as tots'))
 
->rightjoinSub($workers,'worker', function($join){
    $join->on('attendance.wrk_id','=','worker.wrk_id');
})
->groupBy('payroll_attn_id')
->get();

  foreach($attendance as $key){
      $fetch[$key->wrk_id]['wrk_id'] = $key->wrk_id;
      $fetch[$key->wrk_id]['f_name'] = $key->f_name;
      $fetch[$key->wrk_id]['l_name'] = $key->l_name;
      $fetch[$key->wrk_id]['m_name'] = $key->m_name;
      $fetch['total_work_hours'] = $key->tots;
      $fetch[$key->wrk_id]['date'][$key->date]['work_hours'] = $key->reg_hour;
      
  }
  return $fetch;  


}

this is the result total_work_hours display only 8 instead of 56

 2: {wrk_id: 2, f_name: "John", l_name: "Doe", m_name: null,…}
    date: {2020-09-27: {work_hours: 8}, 2020-09-28: {work_hours: 8}, 2020-09-29: {work_hours: 8},…}
    2020-09-27: {work_hours: 8}------------------
    2020-09-28: {work_hours: 8}------------------
    2020-09-29: {work_hours: 8}------------------
    2020-09-30: {work_hours: 8}------------------   TOTAL OF THIS IS 56
    2020-10-01: {work_hours: 8}------------------
    2020-10-02: {work_hours: 8}------------------
    2020-10-03: {work_hours: 8}-----------------
    f_name: "John"
    l_name: "Doe"
    m_name: null
    wrk_id: 2
    total_work_hours: "8" <-------------  56 is the total off work_hours from array

回答1:


try this

 $fetch = [];
    foreach($attendance as $key){
         if(!isset($fetch[$key->wrk_id]['total_work_hours'])){
            $fetch[$key->wrk_id]['total_work_hours'] = 0;
          }
          $fetch[$key->wrk_id]['wrk_id'] = $key->wrk_id;
          $fetch[$key->wrk_id]['f_name'] = $key->f_name;
          $fetch[$key->wrk_id]['l_name'] = $key->l_name;
          $fetch[$key->wrk_id]['m_name'] = $key->m_name;
          $fetch['s'] = $key->tots;
          $fetch[$key->wrk_id]['date'][$key->date]['work_hours'] = $key->reg_hour;
          $fetch[$key->wrk_id]['total_work_hours'] += $key->reg_hour;
          
      }
      return $fetch;  



回答2:


Use foreach like this:

foreach($attendance as $key){
     if(empty($fetch[$key->wrk_id]['total_work_hours'])){
          $fetch[$key->wrk_id]['total_work_hours'] = 0;
     }
      $fetch[$key->wrk_id]['wrk_id'] = $key->wrk_id;
      $fetch[$key->wrk_id]['f_name'] = $key->f_name;
      $fetch[$key->wrk_id]['l_name'] = $key->l_name;
      $fetch[$key->wrk_id]['m_name'] = $key->m_name;
      $fetch['s'] = $key->tots;
      $fetch[$key->wrk_id]['total_work_hours'] += $key->reg_hour;   
  }


来源:https://stackoverflow.com/questions/64021733/how-to-get-sum-of-array-in-foreach-in-laravel-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!