2017-04-07 30 views
0

上下文:我正在嘗試計算員工進行約會的總小時數,並在刀片視圖中顯示員工的總和。我可以成功地總結每個$ EmployeeHours的$ starts_at減$ ends_at日期的小時數。但是,當我dd();我只看到最後一個對象而不是集合。如何在循環foreach之後返回集合或數組數據以查看?

當我嘗試將數據發送到瀏覽在@foreach循環,我得到錯誤:

array_merge(): Argument #2 is not an array

當我保存到一個數據庫中的表,它成功地通過迭代和保存每個$ EmployeeHours我的期望。

問題是我不想將查詢結果保存到數據庫,我只想顯示到刀片服務器,因爲它只是一個用於提取用戶報告的查詢。

$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 as $EmployeeHour) 
     { 

       $duration = []; 

       foreach($EmployeeHour as $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 is where I am going wrong possibly 
      $results = [ 
      'totalHours' => $totalHours,     
      'employee_name' => $collection->first_name. " ".$collection->last_name,      
      ]; 


     } 
      dd($EmployeeHour); 


     return view ('admin.invoices.reporting.employeeHours',$results); 

這裏是刀片觀點

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

我也試過沒有刀片foreach循環,並將其返回的最後一個對象I,E。

<tbody>      
         <tr> 
          <td>{{ $employee_name }}</td> 
          <td>{{ $totalHours }}</td> 
         </tr>       
        </tbody> 
+0

請附上您的問題與您的視圖中的相關代碼。 – user1960364

+0

我已添加視圖代碼。試過2種方法。刀片中的foreach循環返回一個錯誤,大概是因爲我沒有返回一個集合。如果沒有foreach,那麼它會打印最後一個對象。但我想要一個數組。謝謝。 –

回答

1

有兩件事情中脫穎而出,我...

  1. 你更換,而不是追加到它$results
  2. 你正在查看數據被錯誤地傳遞給視圖。

嘗試:

$results = []; 
foreach ($EmployeeHours as $EmployeeHour) { 
    $duration = []; 
    $name = null; 

    foreach ($EmployeeHour as $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 I added [], this will append it to the array instead of replacing it. 
    $results[] = [ 
     'totalHours' => $totalHours, 
     'employee_name' => $name, 
    ]; 
} 

// Notice I used `compact()` which is the same as doing ['results' => $results] 
return view('admin.invoices.reporting.employeeHours', compact('results')); 

我Jono20201同意,你應該重構採取Eloquent ORM的優勢。

+0

我跟着這個,我現在得到一個錯誤:試圖獲得非對象的屬性。 –

+0

哦,這是因爲你的'$ results'被設置在嵌套循環之外...'$ collection'是未定義的。 – user1960364

+0

更新了修復名稱的答案。這不是最有效的方法,但不知道結果集的結構,這是我真正可以建議的。 – user1960364

1

你每段時間設定$results到一個新的數組,當你真正想要添加到$results陣列的每個互爲作用。

$results[] = [ 
    'totalHours' => $totalHours,     
    'employee_name' => $collection->first_name . " " . $collection->last_name,      
]; 

ps。你應該嘗試將這個重構成使用雄辯模型/關係而不是使用查詢生成器。

1

你應該改變這樣的:

$results = [ 
     'totalHours' => $totalHours,     
     'employee_name' => $collection->first_name. " " . $collection->last_name,      
]; 

這樣:

$results[] = [ 
     'totalHours' => $totalHours,     
     'employee_name' => $collection->first_name. " ".$collection->last_name,      
]; 

當你不使用你更換你的數組值到新的值括號,括號內使用你」每次迭代重新添加到數組中的新值。

相關問題