tp框架数据库Db链式操作whereOr

需求:如存在生效中审核中的客户授信申请,不能再申请。

客户授信记录有两个状态:

  • 授信状态:1创建中 2生效中 3已到期 4已关闭 已通过状态手动关闭 5已撤销 审核中撤销申请
  • 审核状态:1审核中 2审核通过 3审核不通过

那么就要用到查询。

原生sql:

select * from credit_apply where customer_id = xxx and (status = 2 or check_status = 1) limit 1;

用tp的链式操作就要用到闭包查询

$result = Db::table('credit_apply')
            ->where('customer_id', $c_id)
            ->where(function ($query) {
                $query->where('status', 2)->whereOr('check_status', 1);
            })
            ->find();

 如果status和check_status是变量,就要用use将外部变量传参

$status = 2;
$check_status = 1;
$result = Db::table('credit_apply')
            ->where('customer_id', $c_id)
            ->where(function ($query) use ($status,$check_status) {
                $query->where('status', $status)->whereOr('check_status', $check_status);
            })
            ->find();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值