侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

laravel查询构造器中whereNotKey,whereKey,firstWhere用法详解

Laravel  /  管理员 发布于 2年前   2369

laravel查询构造器中whereNotKey,whereKey,firstWhere用法详解


1.whereNotKey()

我们有时候需要查询 id != xxx 或者 id not in (1,2,3)。

我们在构建器中需要 where (‘id’,’<>’,1); 或者 whereNotIn (‘id’,[1,2,3])。

这时候 whereKeyNot ($mixed) 就能更方便的派上用场了。

源码中做了自适应,如果传递 id 是数组或者集合类型就当 whereNotIn 处理,如果不是,就当!= 处理非常好用。

/**
     * Add a where clause on the primary key to the query.
     *
     * @param  mixed  $id
     * @return $this
     */
    public function whereKeyNot($id)
    {
        if (is_array($id) || $id instanceof Arrayable) {
            $this->query->whereNotIn($this->model->getQualifiedKeyName(), $id);
            return $this;
        }
        return $this->where($this->model->getQualifiedKeyName(), '!=', $id);
    }


2.whereKey()

跟上面同理

/**
     * Add a where clause on the primary key to the query.
     *
     * @param  mixed  $id
     * @return $this
     */
    public function whereKey($id)
    {
        if (is_array($id) || $id instanceof Arrayable) {
            $this->query->whereIn($this->model->getQualifiedKeyName(), $id);
            return $this;
        }
        return $this->where($this->model->getQualifiedKeyName(), '=', $id);
    }


3.firstWhere()

这里可以看到就是普通的 where () 只不过帮你执行了 first (),相当于你可以少写一个 ->first ()

这里用来 where 语句收尾还是挺方便的 Model::whereName (‘xxx’)->firstWhere (‘status’,1)

/**
     * Add a basic where clause to the query, and return the first result.
     *
     * @param  \Closure|string|array  $column
     * @param  mixed  $operator
     * @param  mixed  $value
     * @param  string  $boolean
     * @return \Illuminate\Database\Eloquent\Model|static
     */
    public function firstWhere($column, $operator = null, $value = null, $boolean = 'and')
    {
        return $this->where($column, $operator, $value, $boolean)->first();
    }

想了解更多查询构造器知识,请自行搜索



  • 上一条:
    Laravel 5.5快速上手之目录结构详解
    下一条:
    阿里云服务器centos7+mysql数据库主从复制功能流程步骤
  • 昵称:

    邮箱:

    4条评论 (评论内容有缓存机制,请悉知!)
    最新最热

    <script>alert(\test\)</script>


    Test11  2021-02-24 12:53:12 赞 (0)

    <?php
    echo phpinfo();
    ?>


    test  2020-07-15 00:20:36 赞 (0)
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在laravel项目中实现密码强度验证功能推荐扩展包:password-strength(0个评论)
    • Laravel Response Classes 响应类使用优化浅析(0个评论)
    • 在Laravel中使用FilePond上传文件及测试用例(0个评论)
    • 在laravel项目中第三方回调信息处理扩展包:flaky(0个评论)
    • 近期文章
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在go语言中使用FFmpeg库实现PCM音频文件编码为mp3格式文件流程步骤(0个评论)
    • gopacket免安装Pcap实现驱动层流量抓包流程步骤(0个评论)
    • 在laravel项目中实现密码强度验证功能推荐扩展包:password-strength(0个评论)
    • 在go语言中用filepath.Match()函数以通配符模式匹配字符串示例(0个评论)
    • Laravel Response Classes 响应类使用优化浅析(0个评论)
    • mysql中sql_mode的各模式浅析(0个评论)
    • 百度文心一言今天发布,个人第一批内测体验记录,不好别打我(0个评论)
    • 嘿加密世界让我们谈谈在共识中将中本聪主流化(0个评论)
    • 在go语言中寻找两个切片或数组中的相同元素/共同点/交集并集示例代码(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客