

我在 App\Reports\ExportReport.php 檔案夾中的代碼
class ReportExport implements FromCollection
{
public function collection()
{
$location_library = \App\Models\LocationLibrary::where('company_id', Auth::user()->company_id)->get();
foreach ($location_library as $key => $locations) {
$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();
}
$object = (object) $cross_kehadiran;
dd($object);
}
}
我希望生成的格式像第一張圖片,但我得到的結果是第二張。我想將陣列轉換為物件。有沒有辦法解決我的問題?幫幫我,謝謝。
uj5u.com熱心網友回復:
我也有這個問題,但我注意到 json_decode 將 JSON 陣列轉換為物件。
$object = json_decode(json_encode($cross_kehadiran));
uj5u.com熱心網友回復:
此函式將回傳一個物件。
function getObjectToArray($object)
{
return @json_decode(json_encode($object), true);
}
uj5u.com熱心網友回復:
這是因為您使用$cross_kehadiran[]的是$cross_kehadiran. 試試這個,你會得到一組物件:
$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();
dd($object);
uj5u.com熱心網友回復:
對于多維陣列
static public function array_to_object(array $array)
{
foreach($array as $key => $value)
{
if(is_array($value))
{
$array[$key] = self::array_to_object($value);
}
}
return (object)$array;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/432545.html
