Home > database >  How to combine return three parameters in laravel collection
How to combine return three parameters in laravel collection

Time:02-22

my code from folder in 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);
}

I'm making export excel, so I want to combine data from the variables $absent, $belum_absen, $cross_kehadiran but here I can only combine $absent and $belum_absen. how to combine $cross_kehadiran as well so all three parameters are combined? help me, thanks!

CodePudding user response:

you can use array_merge function array array_merge($array1, $array2, ......, $arrayn)

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

CodePudding user response:

When you need to merge array you can use:

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

Or when you need to merge collections, you can use:

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

If you want to cast array to collection, example below:

$data = collect($array) and you can merge as collection from array: $data = collect($array1)->merge(collect($array2))->merge(collect($array3));

  • Related