Cannot use object of type IlluminateDatabaseQueryBuilder as array

Tags: , ,

I cannot find the error here how do i fix this?

$db2 = DB::connection('sqlsrv')->table('Checkinout')
    ->select(DB::raw("FORMAT(MAX (Checkinout.CheckTime), 'hh:mm tt') AS OffTime, FORMAT (min(Checkinout.CheckTime), 'hh:mm tt') AS Ontime, Checkinout.Userid,Z_MemRecord.Uname,FORMAT (Checkinout.CheckTime, 'MMM dd yyyy') as dates,FORMAT (Checkinout.CheckTime, 'dddd') as hour"))
    ->groupBy(DB::raw("Convert(Date, Checkinout.CheckTime),Checkinout.Userid,Z_MemRecord.Uname,FORMAT(Checkinout.CheckTime, 'MMM dd yyyy'),FORMAT (Checkinout.CheckTime, 'dddd')"));

$result = DB::connection('mysql')->table('netdoc')->insert(['Logid' => $db2['Checkinout.Userid'] ,'name'=> $db2['Z_MemRecord.Uname'],'dates' =>  $db2['dates'],'day'=>$db2['hour'],'Ontime' => $db2['Ontime'],'Offtime' => $db2['OffTime']]);



The method groupBy() returns a QueryBuilder to convert it to an array call get() to receive an array of rows.


Thou you need to deal with the query returning multiple rows, so an quick solution for that would to traverse db2 with foreach().

foreach ($db2 as $row) {
    $row = (array) $row;

    $result = DB::connection('mysql')
        ->insert(['Logid' => $row['Checkinout.Userid'] ,'name'=> $row['Z_MemRecord.Uname'],'dates' =>  $row['dates'],'day'=>$row['hour'],'Ontime' => $row['Ontime'],'Offtime' => $row['OffTime']]);

Source: stackoverflow