Eirtaza Eirtaza - 2 years ago 169
SQL Question

How to use multiple placeholders (?) in raw Laravel query with in closure

Here is my code...

//Database:: find matching results in feeds table
$users_size = $this->holders($this->followed_users_id);
$slugs_size = $this->holders($this->followed_slugs_id);

$match_query = "select * from feeds " .
"WHERE (user_id IN ($users_size)) " .
"OR (target_id IN ($slugs_size) AND feedable_type = 'review') " .
"ORDER BY created_at DESC;";

$result = DB::select($match_query, $this->followed_users_id, $this->followed_slugs_id);

and don't worry about the (?) placeholders as they are dynamically generated as per requirements and $
are arrays

Result is this

QueryException in Connection.php line 655:
SQLSTATE[HY093]: Invalid parameter number (SQL: select * from feeds WHERE (user_id IN (1,17)) OR (target_id IN (?,?) AND feedable_type = 'review') ORDER BY created_at DESC;)

Answer Source

I think the bindings have to be in an array and as a second parameter in the select method

$bindings = array_merge($this->followed_users_id, $this->followed_slugs_id);

$result = DB::select($match_query, $bindings);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download