Total hours of 1 user gets shown on every user PHP Laravel -


i having problem not understanding why comes so. code:

function gethours($project_id, $project_startdate, $project_enddate) {     $project = project::find($project_id);     $users = $project->users()->get();      $start_date = $project_startdate;     $end_date = $project_enddate;      foreach ($users $user) {         $timesheethours = $user->timesheets()->wherebetween('date', [$start_date, $end_date])->get()->sum('hours');     }     return $timesheethours;  } 

i have 2 users in, , both of them have different hours, shows same hours them all. tried many ways, foreach timesheethours (ofc code not above). missing?

you overwrite variable $timesheethours in second loop, must use array.

 foreach ($users $user) {         $timesheethours[] = $user->timesheets()->wherebetween('date', [$start_date, $end_date])->get()->sum('hours');  }  return $timesheethours; 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -