簡體   English   中英

laravel集合中如何組合返回三個參數

[英]How to combine return three parameters in laravel collection

我的代碼來自 App\Reports\ExportReport.php 中的文件夾

class ReportExport implements FromCollection
{
  $absen = DB::table('attendance as in')
                            ->where('in.in_out', 'in')
                            ->where('in.company_id', \Session::get('selected_company'))
                            ->whereDate('in.created', $date)
                            ->leftJoin('attendance as out', function ($join) use ($date) {
                                $join->on('in.employee_id', 'out.employee_id')
                                    ->where('out.in_out', 'out')
                                    ->where('out.company_id', \Session::get('selected_company'))
                                    ->whereDate('out.created', $date);
                            })
                            ->join('employee', 'employee.id', 'in.employee_id')
                            ->join('location_library', 'location_library.id', 'in.attendance_location_id')
                            ->join('company as cp', 'cp.id', 'in.company_id')
                            ->join('employee_in_app as e_app', 'e_app.employee_id', 'in.employee_id')
                            ->join('employee_in_company', 'in.employee_id', 'employee_in_company.employee_id')
                            ->select('in.id', 'in.employee_id', 'in.attendance_time as in_time', 'out.attendance_time as out_time', 'in.work_hour_start', 'in.late_tolerance', 'employee.*', 'location_library.location_name', 'in.attendance_location_id', 'cp.alias', 'e_app.note', 'in.company_id', 'in.attendance_time', 'employee_in_company.ignore_work_hour', 'employee_in_company.attendance_group_id')
                            ->orderBy('in.attendance_time', 'DESC')
                            ->get();

  $belum_absen = EmployeeInCompany::where('company_id', \Session::get('selected_company'))
                        ->join('company', 'company.id', 'employee_in_company.company_id')
                        ->join('employee', 'employee.id', 'employee_in_company.employee_id')
                        ->whereNotIn('employee.id', $data)
                        ->select('employee.name','company.alias','employee.id')
                        ->orderBy('employee.name', 'asc')
                        ->get();

  $cross_kehadiran[] = DB::table('attendance as in')
                        ->where('in.in_out', 'in')
                        ->where('in.attendance_location_id', $locations->id)
                        ->where('in.company_id', '!=', \Session::get('selected_company'))
                        ->whereDate('in.created', Carbon::today())
                        ->leftJoin('attendance as out', function ($join) {
                            $join->on('in.employee_id', 'out.employee_id')
                                ->where('out.in_out', 'out')
                                ->where('out.attendance_location_id', 'in.attendance_location_id')
                                ->whereDate('out.created', Carbon::today());
                        })
                        ->join('employee', 'employee.id', 'in.employee_id')
                        ->join('location_library', 'location_library.id', 'in.attendance_location_id')
                        ->join('company as cp', 'cp.id', 'in.company_id')
                        ->join('employee_in_app as e_app', 'e_app.employee_id', 'in.employee_id')
                        ->select('employee.name', 'cp.alias', 'in.employee_id','location_library.location_name',  DB::raw('DATE_FORMAT(in.attendance_time, "%H:%i:%s") as in_time'), DB::raw('DATE_FORMAT(out.attendance_time, "%H:%i:%s") as out_time'), 'e_app.note')
                        ->orderBy('in.attendance_time', 'DESC')
                        ->get();

       return $absen->merge($belum_absen);
}

我正在導出 excel,所以我想合並來自變量 $absent、$belum_absen、$cross_kehadiran 的數據,但在這里我只能合並 $absent 和 $belum_absen。 如何結合 $cross_kehadiran 以及如何結合所有三個參數? 幫幫我,謝謝!

你可以使用array_merge function array array_merge($array1, $array2, ......, $arrayn)

$data=array_merge($cross_kehadiran, [$absen]);

當你需要合並數組時,你可以使用:

$data = array_merge($array1, $array2, $array3);

或者當你需要合並collections時,你可以使用:

$data = $collection1->merge($collection2)->merge($collection3)

如果要將數組轉換為集合,示例如下:

$data = collect($array)並且您可以合並為數組中的集合: $data = collect($array1)->merge(collect($array2))->merge(collect($array3));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM