1

Code

$loans2ndChart = Loan::select('date_release')
                         ->where('invalid',false)
                         ->where('transaction_year', $transyear)
                         ->get()
                         ->groupBy(function($date) {
                  
                     return Carbon::parse($date->date_release)->format('m'); // grouping by months
              });

    $chart2Array = [];
    $loanChartArr = [];

    foreach ($loans2ndChart as $key => $value) {
        $chart2Array[(int)$key] = count($value);
    }

for($j = 1; $j <= 12; $j++){
    if(!empty($chart2Array[$j])){
      array_push($loanChartArr,array($loanChartArr[$j],$chart2Array[$j]));  
    }else{
      array_push($loanChartArr,array($loanChartArr[$j],0)); 
    }
}

$loanTypeCount = Loan::select(['loan_type AS name', DB::raw('count(loan_type) AS y')])
                         ->groupBy('loan_type')
                         ->orderBy(DB::raw('y'), 'desc')
                         ->where('transaction_year', $transyear)
                         ->where('invalid', false)
                         ->get();                
                    
    $loanTypeCount->map(function($loan)use($loanChartArr) {
        $loan->sliced = true;
        $loan->selected = true;
        $loan->data = $loanChartArr;
        return $loan;
    });

enter image description here

Output: var_dump($chart2Array );

array(5) { [9]=> int(1) [5]=> int(2) [6]=> int(1) [7]=> int(3) [8]=> int(1) }

...and when inserted isset in array

for($j = 1; $j <= 12; $j++){
    if(!empty($chart2Array[$j])){
      array_push($loanChartArr,array(isset($loanChartArr[$j]),$chart2Array[$j]));  
    }else{
      array_push($loanChartArr,array(isset($loanChartArr[$j]),0)); 
    }
}

Output: var_dump($loanTypeCount );

string(568) "[{"name":"Salary","y":"6","sliced":true,"selected":true,"data":[[false,0],[false,0],[false,0],[false,0],[false,2],[false,1],[false,3],[false,1],[false,1],[false,0],[false,0],[false,0]]},{"name":"Emergency","y":"1","sliced":true,"selected":true,"data":[[false,0],[false,0],[false,0],[false,0],[false,2],[false,1],[false,3],[false,1],[false,1],[false,0],[false,0],[false,0]]}]"

Desired Output:

string(568) "[{"name":"Salary","y":"6","sliced":true,"selected":true,"data":[[1,0],[2,0],[3,0],[4,0],[5,2],[6,1],[7,3],[8,1],[9,1],[10,0],[11,0],[12,0]]},{"name":"Emergency","y":"1","sliced":true,"selected":true,"data":[[1,0],[2,0],[3,0],[4,0],[5,2],[6,1],[7,3],[8,1],[9,1],[10,0],[11,0],[12,0]]}]"

What is the right way to do this? Thank you.

Eli
  • 1,256
  • 4
  • 29
  • 59
  • Please let me know what you want to accomplish as well the data inside the loans2ndChart you receive after the query ? – Muhammad Atif Akram Oct 02 '21 at 04:45
  • 1
    The key 1 doesn't exist in the array `$loanChartArr` – linktoahref Oct 02 '21 at 05:24
  • But why it works whenever I use this one? for($j = 1; $j <= 12; $j++){ if(!empty($chart2Array[$j])){ array_push($loanChartArr,array(isset($loanChartArr[$j]),$chart2Array[$j])); }else{ array_push($loanChartArr,array(isset($loanChartArr[$j]),0)); } – Eli Oct 02 '21 at 06:01
  • The problem is there is no key 1 because the query has no january month in the database. – Eli Oct 02 '21 at 07:28

1 Answers1

0

In the first attempt in the for loop $loanChartArr[$j] is not defined yet, this is why you're getting undefined offset error

$loanChartArr = [];
...

for($j = 1; $j <= 12; $j++){
    if(!empty($chart2Array[$j])){
      array_push($loanChartArr,array($loanChartArr[$j],$chart2Array[$j]));  
    }else{
      array_push($loanChartArr,array($loanChartArr[$j],0)); // <=  $loanChartArr[$j] is not defined yet 
    }
}
mwafi
  • 3,946
  • 8
  • 56
  • 83