my code from folder in 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);
}
}
I want the resulting format to be like image number one but the result I get is number two. I want to convert an array into an object. is there a way to solve my problem? help me, thanks.
CodePudding user response:
I also had this issue, but I noticed that json_decode converts JSON array to object.
$object = json_decode(json_encode($cross_kehadiran));
CodePudding user response:
This function will return an object.
function getObjectToArray($object)
{
return @json_decode(json_encode($object), true);
}
CodePudding user response:
It's because you're using $cross_kehadiran[]
instead of $cross_kehadiran
. Just try this one and you get array of objects:
$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);
CodePudding user response:
For multi-dimentional arrays
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;
}