0

My goal is to convert the following SQL query to to Laravel query builder syntax,

    $qstA = "
            select sum(tmpbutbldata.Personnel_Hours_Spent) as PHS from tmpbutbldata 
            left join tbl_testlocation_links on tmpbutbldata.Test_Request_Number= tbl_testlocation_links.Test_Request_number 
            where 
            tmpbutbldata.Date_Test_Completed between '".$dateBack."' and '".$dateCurr."' 
            and tbl_testlocation_links.".$Location."='1' 
            and tmpbutbldata.type = '1' 
            and tmpbutbldata.cancelled = '0' 
            or 
            tmpbutbldata.Date_Test_Completed between '".$dateBack."' and '".$dateCurr."' 
            and tbl_testlocation_links.".$Location."='1' 
            and tmpbutbldata.type = '2' 
            and tmpbutbldata.cancelled = '0'"
    ;

what i've tried:

DB::table("tbldata")
        ->select(DB::raw('*'))
        ->leftJoin('tbl_testlocation_links', 'tbldata.Test_Request_Number', '=', 'tbl_testlocation_links.Test_Request_number')
        ->where(['Date_Test_Completed', '<', $dateBack],
                ['Date_Test_Completed', '>', $dateCurr],
                ['tbl_testlocation_links'.$location->TestLocation, '=', 1],
                ['type', '=', 1],
                ['cancelled', '=', 0])
        ->orWhere(['Date_Test_Completed', '<', $dateBack],
                ['Date_Test_Completed', '>', $dateCurr],
                ['tbl_testlocation_links'.$location->TestLocation, '=', 1],
                ['type', '=', 2],
                ['cancelled', '=', 0])
        ->get();

But I get the following error ErrorException Array to string conversion

miken32
  • 42,008
  • 16
  • 111
  • 154
salah1337
  • 67
  • 1
  • 7

1 Answers1

1

Try

DB::table("tbldata")
        ->select(DB::raw('*'))
        ->leftJoin('tbl_testlocation_links', 'tbldata.Test_Request_Number', '=', 'tbl_testlocation_links.Test_Request_number')
        ->where([
             ['Date_Test_Completed', '<', $dateBack],
             ['Date_Test_Completed', '>', $dateCurr],
             ['tbl_testlocation_links'.$location->TestLocation, '=', 1],
             ['type', '=', 1],
             ['cancelled', '=', 0]
        ])
        ->orWhere([
             ['Date_Test_Completed', '<', $dateBack],
             ['Date_Test_Completed', '>', $dateCurr],
             ['tbl_testlocation_links'.$location->TestLocation, '=', 1],
             ['type', '=', 2],
             ['cancelled', '=', 0]
        ])
        ->get();

Multiple conditions must be passed in a single where clause as an array of arrays, where each nested array represents a condition

Donkarnash
  • 12,433
  • 5
  • 26
  • 37