sql - How to return collection or array data to view after looping through foreach? -


context: trying work out total number of hours employee has worked appointment , show sum employee in blade view. can sum hours $starts_at minus $ends_at dates each each $employeehours. when dd(); see last object , not collection.

when try send data view in @foreach loop error:

array_merge(): argument #2 not array

when save table in db, iterates through , saves each $employeehours expect.

problem not want save results of query db, want display blade only query user report extracted.

$employeehours = db::table('appointment_employee')         ->join('appointments', 'appointment_id', '=', 'appointments.id')         ->join('employees', 'employees.id', '=', 'appointment_employee.employee_id')         ->join('users', 'users.id', '=', 'employees.user_id')         ->where('role_id', '=', '1')         ->get()->groupby('id');          $results=[];         foreach($employeehours $employeehour)         {                  $duration = [];                  foreach($employeehour $collection)                  {                  $date1      = $collection->starts_at;                  $date2      = $collection->ends_at;                  $start      = carbon::parse($date1);                 $end        = carbon::parse($date2);                  $length     = $start->diffinminutes($end)/60;                 $duration[] = $length;                    }                  $totalhours = array_sum($duration);           //i think here going wrong possibly             $results = [             'totalhours' => $totalhours,                             'employee_name' => $collection->first_name. " ".$collection->last_name,                                  ];             }                dd($employeehour);             return view ('admin.invoices.reporting.employeehours',$results); 

here blade view

@foreach($results $result)                         <tr>                             <td>{{ $result->employee_name }}</td>                             <td>{{ $result->totalhours }}</td>                         </tr>                     @endforeach  

i have tried without foreach loop in blade , returns last object i,e .

<tbody>                                              <tr>                             <td>{{ $employee_name }}</td>                             <td>{{ $totalhours }}</td>                         </tr>                                                </tbody> 

there 2 things stand out me...

  1. you're replacing $results instead of appending it.
  2. you're view data being passed view incorrectly.

try:

$results = []; foreach ($employeehours $employeehour) {     $duration = [];     $name = null;      foreach ($employeehour $collection) {         $date1      = $collection->starts_at;         $date2      = $collection->ends_at;          $start      = carbon::parse($date1);         $end        = carbon::parse($date2);          $length     = $start->diffinminutes($end)/60;         $duration[] = $length;          if (is_null($name)) {             $name = $collection->first_name . " " . $collection->last_name;         }     }      $totalhours = array_sum($duration);      // notice added [], append array instead of replacing it.     $results[] = [         'totalhours'    => $totalhours,         'employee_name' => $name,     ]; }  // notice used `compact()` same doing ['results' => $results] return view('admin.invoices.reporting.employeehours', compact('results')); 

i agree jono20201, should refactor take advantage of eloquent orm.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -