Skip to content
Advertisement

Cannot use object of type IlluminateDatabaseQueryBuilder as array

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

$db2 = DB::connection('sqlsrv')->table('Checkinout')
    ->join('Z_MemRecord','Checkinout.Userid','=','Z_MemRecord.uid')
    ->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']]);


dd($db2);

Advertisement

Answer

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

->groupBy(...)
->get();

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')
        ->table('netdoc')
        ->insert(['Logid' => $row['Checkinout.Userid'] ,'name'=> $row['Z_MemRecord.Uname'],'dates' =>  $row['dates'],'day'=>$row['hour'],'Ontime' => $row['Ontime'],'Offtime' => $row['OffTime']]);
} 
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement