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

问题描述 投票:0回答:1

这是我的代码...

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

php mysql laravel eloquent
1个回答
0
投票
我认为绑定必须位于数组中并作为

select

 方法中的第二个参数

$bindings = array_merge($this->followed_users_id, $this->followed_slugs_id); $result = DB::select($match_query, $bindings);
    
© www.soinside.com 2019 - 2024. All rights reserved.