laravel Eloquent集合where方法不支持<,>,<>等运算符

Song2851 次浏览1个评论2018年04月04日

今天在查询系统bug的过程中,发现laravel Eloquent集合where方法不支持<,>,<>等运算符,比如:

$collection = collect([
    ['product' => 'Desk', 'price' => 200],
    ['product' => 'Chair', 'price' => 100],
    ['product' => 'Bookcase', 'price' => 150],
    ['product' => 'Door', 'price' => 100],
]);

$filtered = $collection->where('price','<>',100);

$filtered->all();

上面的算法中,你会发现会返回空数据,查看源码可以发现:

public function where($key, $value, $strict = true)
    {
        return $this->filter(function ($item) use ($key, $value, $strict) {
            return $strict ? data_get($item, $key) === $value
                           : data_get($item, $key) == $value;
        });
    }

laravel Eloquent集合where方法不支持<,>,<>等运算符,所以大家需要自己更改自己的查询!

提交评论

请登录后评论

用户评论

  • Song Song 2017-07-16 04:00:58
    我们的版本怕是不一样。。 *Laravel 5.5里面Collection.php line 459* ``` public function where($key, $operator, $value = null) { return $this->filter($this->operatorForWhere(...func_get_args())); } ```
    0 赞 0 条评论 回复
    评论
    查看更多评论!

更多相关好文

    当前暂无更多相关好文推荐...