如何在循环遍历foreach后返回集合或数组数据?

时间:2017-04-07 19:48:57

标签: sql laravel laravel-5.3

背景信息:我正在尝试计算员工预约的总小时数,并在刀片视图中按员工显示总和。我可以成功总结每个$ EmployeeHours的$ starts_at减去$ ends_at日期的小时数。但是当我dd();我只看到最后一个对象,而不是一个集合。

当我尝试将数据发送到@foreach循环中查看时,我收到错误:

  

array_merge():参数#2不是数组

当我保存到数据库中的表时,它成功迭代并保存我期望的每个$ EmployeeHours。

问题是我不想将查询结果保存到DB,我只想显示到刀片,因为它只是查询要提取的用户报告。

$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>

3 个答案:

答案 0 :(得分:1)

您每次都要将$results设置为新数组,而实际上您想要在每次交互时添加到$results数组。

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

PS。您应该尝试使用Eloquent Models / Relationships重构它,而不是使用查询构建器。

答案 1 :(得分:1)

有两件事让我感到高兴......

  1. 您正在替换$results而不是附加到它。
  2. 您的视图数据传输错误。
  3. 尝试:

    $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

答案 2 :(得分:1)

你应该改变这个:

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

到此:

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

如果不使用括号,则将数组值替换为新值,使用括号,每次迭代都会将数组附加到数组中。