laravel 模型集合where

PHP是一个弱类型的编程语言,所以从mysql数据库查询出来数据都会默认为string类型,具体原因及解释,可看这边文章https://blog.csdn.net/Darry_Zhao/article/details/52914245

所以当使用laravel集合模型的where方法进行筛选时,需注意:

1:5.2及以下laravel版本,where()为严格匹配模式,源码如下:

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

 public function whereLoose($key, $value)
    {
        return $this->where($key, $value, false);
    }

所以使用时可以用where($key,$value,false),传第三个为false,或直接使用laravel自身封装whereLoose(其实都一样)

2.5.3及以上laravel版本,where()为非严格匹配(whereLoose废弃),想用严格匹配也可直接使用whereStrict()

3.对于数组类的条件筛选时,可用whereIn和whereInloose(5.2及以上有这两个方法),但项目中发现laravel5.2在本地使用这两个方法是可以的,但到正式环境没用,具体原因暂不清楚,别的5.2以上的laravel版本还没测试过(大家使用时注意一下)

猜你喜欢

转载自blog.csdn.net/king2wang/article/details/85678599