$absent 變數 的資料結果 $belum_absen 變數的資料結果
我在 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;
}
我正在制作匯出 excel,所以我想組合來自變數 $absent 和 $belum_absen 的資料,但是我收到錯誤“array_merge(): Expected parameter 1 to be an array, object given”有沒有辦法解決這個問題問題?幫幫我,謝謝
uj5u.com熱心網友回復:
這些查詢回傳集合,而不是陣列。所以你不能對它們使用陣列函式。Laravel 有非常有用的 Collection 方法。我建議你看看他們。https://laravel.com/docs/9.x/collections
你需要的方法是
return $absen->merge($belum_absen);
uj5u.com熱心網友回復:
您不能使用 array_merge 來合并多個集合。您應該在 get() 之后使用 ->toArray() 將集合資料轉換為陣列。之后你可以使用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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/430649.html
下一篇:使用numpy組合2個陣列
