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

4
  • On which line do you get the error? Commented Dec 4, 2020 at 2:21
  • on the ->get() line Commented Dec 4, 2020 at 2:21
  • 1
    Does this answer your question? How to Create Multiple Where Clause Query Using Laravel Eloquent? Commented Dec 4, 2020 at 2:50
  • Yup, thank you for the help! Commented Dec 4, 2020 at 3:02

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

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.