i'm trying to do average in each row from my database table. but it is not working properly i wanted to ignore the value if null it will not calculate as zero. with my code it is calculating the null value as zero i want to do it like MS Excel where if row/cell is empty it will ignore.
Controller code:
public function update_average(Request $request)
{
$scores = $request->input('scores'); //here scores is the input array param
foreach($scores as $row){
$score = cie_score::find($row['id']);
$score->term2_average =round( ($row['term1_result'?: null] + $row['term2_result'?: null]) /2);
$score->term3_average =round( ($row['term1_result'?: null] + $row['term2_result'?: null] + $row['term3_result'?: null])/3);
$score->term4_average =round( ($row['term1_result'?: null] + $row['term2_result'?: null] + $row['term3_result'?: null] + $row['term4_result'?: null])/4);
$score->save();
}
return redirect()->back();
}
