Home > Software design >  ErrorException array_merge(): Expected parameter 1 to be an array, object given
ErrorException array_merge(): Expected parameter 1 to be an array, object given

Time:02-21

the result of the data from the $absent variable the result of the data from the $belum_absen variable

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

      $combine = array_merge($absen, $belum_absen);
      return $combine;
}
 

I'm making export excel, so I want to combine data from the variable $absent and $belum_absen, but I get an error "array_merge(): Expected parameter 1 to be an array, object given " is there a way to solve this problem? help me, thanks

CodePudding user response:

Those querries return collections, not arrays. So you can't use array functions with them. Laravel has very helpful Collection methods. I suggest you take a look at them. https://laravel.com/docs/9.x/collections

The method you need is

return $absen->merge($belum_absen);

CodePudding user response:

you can't use array_merge for merging multiple collections. you should use ->toArray() after get() for convert the collection data to array. after that you can use array_merge.

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()->toArray();

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

      $combine = array_merge($absen, $belum_absen);
      return $combine;
}
  • Related