Hello I have a problem with PHP laravel with JSON I am saving in filled
Problem I want show user data and get sum of degree for each user how can I do this ?
This is my data:
| Id | User | Month | Element_degree |
|---|---|---|---|
| 13 | 2 | 2 | "{"13":"122","14":"130"}" |
| 14 | 3 | 2 | "{"13":"100","14":"120"}" |
| 15 | 4 | 2 | "{"13":"140","14":"100"}" |
And this is my code in controller:
$data=empdata::all()
How can I get a sum of degrees for each row from field element_degree for each user ?
try this code it will add element_degree_total field in your emp data for each data
$employees = empdata::all();
foreach($employees as $employee){
$data = json_decode($employee->element_degree);
$total = 0;
foreach ($data as $d) {
$total += $d;
}
$employee->element_degree_total = $total;
}