如何在闭包中使用原始Laravel查询中的多个占位符(?)

时间:2016-05-23 13:28:07

标签: php mysql laravel eloquent

这是我的代码......

//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);

并且不要担心(?)占位符,因为它们是根据要求动态生成的,$ this->followed_users_id$this->followed_slugs_id是数组

结果就是这个

  

Connection.php第655行中的QueryException:      SQLSTATE [HY093]:参数号无效(SQL:select * from feeds WHERE(user_id IN(1,17))OR(target_id IN(?,?)AND feedable_type ='review')ORDER BY created_at DESC;)

1 个答案:

答案 0 :(得分:0)

我认为绑定必须在数组中,并作为select方法中的第二个参数

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

$result = DB::select($match_query, $bindings);