whereBetween 传递数组

whereBetween passing array

您好,我有一个查询,我想将一个数组传递给 whereBetween 查询。

例如我有一个看起来像这样的数组

Array
(
    [product_id] => Array
        (
            [0] => 31337
            [1] => 31366
        )

    [lands] => Array
        (
            [0] => 12
            [1] => 23
        )

)

现在我想搜索那些 product_id 在 [0] => 31337 和 [1] => 31366 之间的土地我想找到在 [0] => 12 之间的土地和 [1] => 23

现在说我有一个变量 $filters,其中包含上面的数组,我将它像这样传递给如下查询。

public function scopeDynamicInBetweenFilter($query, $filters)
    {
        if(!empty($filters)){
            return $query->whereBetween($filters);
        }
        return $query;
    }

它给我一个错误

Type error: Too few arguments to function Illuminate\Database\Query\Builder::whereBetween(), 1 passed and at least 2 expected

它在

确实如此
Builder->whereBetween('product_id' => array('31337', '31366'))->whereBetween('lands' => array('12', '23'))

可以做些什么来实现这一目标。

试试这个:

if(!empty($filters)){
    return $query->whereBetween('product_id', $filters['product_id'])->whereBetween('lands', $filters['lands']);
}

您可以尝试遍历过滤器并将其应用于查询

public function scopeDynamicInBetweenFilter($query, $filters)
{
    if(! empty($filters)) {
        if (is_array($filters)) {
            foreach ($filters as $key => $value) {
                $query->whereBetween($key, $value);
            }
        }
    }

    return $query;
}