foreach循环后如何return collection或数组数据查看?

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

上下文:我正在尝试计算出员工在约会中工作的总小时数,并在 blade 视图中按员工显示总数。我可以成功地总结每个 $EmployeeHours 的 $starts_at 减去 $ends_at 日期的小时数。但是当我 dd();我只看到最后一个 object 而不是 collection.

当我尝试发送数据以在@foreach 循环中查看时出现错误:

array_merge(): Argument #2 is not an array

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

问题是我不想将查询结果保存到数据库,我只想显示到 blade,因为它只是对要提取的用户报告的查询。

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

这是 blade 视图

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

我也试过 blade 中没有 foreach 循环,它 returns 最后一个 object i,e .

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

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

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

ps。您真的应该尝试将其重构为使用 Eloquent Models/Relationships 而不是使用查询生成器。

有两件事让我印象深刻...

  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

你应该改变这个:

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

对此:

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

当您不使用括号时,您将数组值替换为新值,使用括号时,您将在每次迭代时将新值附加到数组。