侯体宗的博客 laravel查询构造器中whereNotKey,whereKey,firstWhere用法详解

石可破,丹可磨
  • 首页
  • laravel8仿版
  • beego仿版
  • go_聊天
  • 人生
  • 技术
  • php
  • 架构
  • 数据库
  • 更多
    • 文件下载
    • 匿名群聊
    • 群聊(进来吹会!)
    • 留言
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册
    • 设置栏目
    • 更多设置
    • 分割线

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

Laravel  /  管理员 发布于 2020-07-06 10:44:45   1009

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 赞 (1)

<?php
echo phpinfo();
?>


test  2020-07-15 00:20:36 赞 (0)
  • 分类目录
  • 人生 (119)
  • 技术 (49)
  • linux (25)
  • blog从零开始 (9)
  • php (48)
  • 架构 (15)
  • 前端 (22)
  • TP(3/5) (14)
  • 数据库 (29)
  • 微信 (2)
  • Laravel (58)
  • Redis (3)
  • Docker (2)
  • Go (8)
  • swoole (7)
  • 相关文章
  • 如何才能使一个良好的结构在Laravel框架项目中呢?介绍一下HMVC模式跟nwidart/laravel modules这个包(0个评论)
  • Laravel原生支持Swoole了,介绍这个包Laravel Octane(0个评论)
  • 在Laravel中进行类型转换详解(0个评论)
  • JWT源码实现逻辑详解(0个评论)
  • Laravel内核分析-设计模式之观察者模式(0个评论)
  • 近期文章
  • 国内用什么翻墙使用谷歌?上外网神器佛跳墙VPN(永久免费)使用流程步骤(0个评论)
  • 在Linux系统中Shell编程的基本语法详解(0个评论)
  • Linux系统中常用的、基本的操作命令使用说明详解(0个评论)
  • Hyperf2.1框架基于session的登录注册退出等个人中心功能的开发流程步骤(0个评论)
  • 下单减库存与付款减库存的三种扣减库存方案优缺点及其使用场景探讨(0个评论)
  • Hyperf2.1启动报错:failed to listen server port[0.0.0.0:9501], Error: Address already(0个评论)
  • 国内用什么翻墙使用谷歌?上外网神器集装箱chrome扩展插件详解(0个评论)
  • 如何才能使一个良好的结构在Laravel框架项目中呢?介绍一下HMVC模式跟nwidart/laravel modules这个包(0个评论)
  • Hyperf2.1框架中验证器的使用流程步骤(0个评论)
  • Hyperf2.1版本自定义函数的编写流程步骤及使用(0个评论)
  • 近期评论
  • 博主 在

    nginx中500,501,502,503,504,505状态码的详解及出现的原因/区别中评论 有错误可以指正吧 没必要搞人心态 老铁 信不信我删库跑路..
  • 博主 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 @白马  最近是很不稳定,后面我会找找其它更稳定的方式,发表一篇博文..
  • 白马 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 显示:暂停注册。。这注册不了啊,该怎么办呢?..
  • 博主 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 @请教  小图标没有出现 重复检查步骤5,进去看看右下角是否开启..
  • 请教 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 你好,我也遇到了安装完右上角没有显示图标,也不能打开相关网页的问题,用的是谷歌浏..
  • 文章归档
  • 2020-04 (5)
  • 2020-05 (6)
  • 2020-06 (7)
  • 2020-07 (8)
  • 2020-08 (4)
  • 2020-09 (7)
  • 2020-10 (6)
  • 2020-11 (6)
  • 2021-01 (4)
  • 2021-02 (2)
  • 2021-03 (1)
  • 2021-04 (1)
Top
  • 友情链接
  • 技术博客集
  • 澜溪博客
  • 心中hope
  • 陈大剩博客
  • 赵波的博客
  • 佘春晓的博客
  • Liao-博客
  • 友链交换留言

Auther ·HouTiZong© 2009-2020 zongscan.com 版权所有ICP证: 粤ICP备20027696号 PHP交流群 也可以扫右边的二维码

侯体宗的博客