2

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);
3
  • Which dbms are you using? db2, mysql and sqlsrv give different indications. Commented Jan 22, 2020 at 9:10
  • i am using SQL Server MS 2018 and i just want to insert the modified data from sqlsrv to mysql. Commented Jan 22, 2020 at 9:12
  • dd($db2); before your $result. As the message says, it's a Builder object, you probably need to get results first using ->get() first to access Checkinout.Userid Commented Jan 22, 2020 at 9:18

1 Answer 1

2

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']]);
} 
Sign up to request clarification or add additional context in comments.

8 Comments

now it shows this error "Cannot use object of type stdClass as array".
Laravel returns stdObjects i converted it to array i guess it will help :)
It works now! It does pass the all data i need! That's really a big help! omg. thank you very much. :D
ahm, one last question. how do i fix the duplicate something? "Integrity constraint violation: 1062 Duplicate entry '40218' for key 'PRIMARY'"
What is your primary key? usually that is set by the db
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.