title: How to return collection or array data to view after looping through foreach?
body: <p>Context: I am trying work out the total number of hours an employee has worked an appointment and show the sum by employee in the blade view. I can successfully sum up the hours for $starts_at minus $ends_at dates for each each $EmployeeHours. However when I dd(); i only see the last object and not a collection.</p>
  
  <p>When I try to send data to view in a @foreach loop I get error:</p>
  
  <blockquote>
    <p>array_merge(): Argument #2 is not an array</p>
  </blockquote>
  
  <p>When I save to a table in the DB, it successfully iterates through and saves each $EmployeeHours I expect. </p>
  
  <p>Problem is I do not want to save the results of the query to DB, I want to display to blade only as it is only a query for a user report to be extracted. </p>
  
  <pre><code>$EmployeeHours = DB::table('appointment_employee')
          -&gt;join('appointments', 'appointment_id', '=', 'appointments.id')
          -&gt;join('employees', 'employees.id', '=', 'appointment_employee.employee_id')
          -&gt;join('users', 'users.id', '=', 'employees.user_id')
          -&gt;where('role_id', '=', '1')
          -&gt;get()-&gt;groupby('id');
  
          $results=[];
          foreach($EmployeeHours as $EmployeeHour)
          {
  
                  $duration = [];
  
                  foreach($EmployeeHour as $collection) 
                  {
  
                  $date1      = $collection-&gt;starts_at; 
                  $date2      = $collection-&gt;ends_at;
  
                  $start      = Carbon::parse($date1);
                  $end        = Carbon::parse($date2);
  
                  $length     = $start-&gt;diffInMinutes($end)/60;
                  $duration[] = $length;  
  
                  }
  
                  $totalHours = array_sum($duration);         
  
  //I think here is where I am going wrong possibly
              $results = [
              'totalHours' =&gt; $totalHours,                
              'employee_name' =&gt; $collection-&gt;first_name. " ".$collection-&gt;last_name,                     
              ];  
  
  
          }   
              dd($EmployeeHour);  
  
  
          return view ('admin.invoices.reporting.employeeHours',$results);
  </code></pre>
  
  <p>Here is the blade view</p>
  
  <pre><code>@foreach($results as $result)
                          &lt;tr&gt;
                              &lt;td&gt;{{ $result-&gt;employee_name }}&lt;/td&gt;
                              &lt;td&gt;{{ $result-&gt;totalHours }}&lt;/td&gt;
                          &lt;/tr&gt;
                      @endforeach 
  </code></pre>
  
  <p>I have also tried without foreach loop in blade and it returns the last object i,e .</p>
  
  <pre><code>&lt;tbody&gt;                     
                          &lt;tr&gt;
                              &lt;td&gt;{{ $employee_name }}&lt;/td&gt;
                              &lt;td&gt;{{ $totalHours }}&lt;/td&gt;
                          &lt;/tr&gt;                           
                      &lt;/tbody&gt;
  </code></pre>