0

I want to predifine $processingStatus array and use it inside Laravel DB::raw query but this gives me the following error-

Syntax error or access violation: 1064 You have an error in your SQL syntax

My query is a large one so I am only pasting the fragment which I guess is causing the error-

DB::raw("COUNT(CASE WHEN (status_change_log.status in $processingStatus) THEN 1 END) AS processing")

My processingStatus variable contains [0 => "S2", 1 => "S13", 2 => "S21"]

Anyone help me out with proper format?

1 Answer 1

4

Concatenate the string with the php variable

DB::raw("COUNT(CASE WHEN (status_change_log.status in ".$processingStatus.") THEN 1 END) AS processing")
Sign up to request clarification or add additional context in comments.

4 Comments

This doesnt work as it gives Array to string conversion error.
that means the $processingStatus variable is an Array, could you give me more info on what you need from that variable? what field, or print in console that variable, so you see what is it, you need it to be a string to use that value in an SQL statement.
Yes of course. There are some status stored in that variable and the query has to run dynamically. Thats why I have to use a variable. I have edited my qstn and mentioned the elements of the array. Hope that helps.
I needed to convert the array to string and after that it worked. Thanks for your suggestion.

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.