In my Laravel project (version 5.3), I’m sending a param in a URL:
&container=0
But by the time it gets into the PHP function that creates an SQL query, it treats the zero as empty and satisfies the empty string portion of this line:
$containerCL = empty($container) ? '' : "AND SHPTABLE.CNT IN (".implode(',',$container).")";
I do want to send an empty string into the query if no option is chosen for this, but in this case, I need to send the zero value as we have some records identified by that being the actual value. How can I let this catch empty parameters but still use zero as an actual value for the query?
2
Answers
It seems like the issue is with the
implode
function when$container
is0
. Theimplode
function expects an array, but if$container
is0
, it’s not an array and thusimplode
returns an empty string, causing the syntax error.You can check if
$container
is an array before callingimplode
. You can cast it to an array if it’s not an array. This will ensure thatimplode
always receives an array, even if$container
is0
.make use of isset() function.
$containerCL = isset($container) ? ” : "AND SHPTABLE.CNT IN (".implode(‘,’,$container).")";