tp hasWhere

hasWhere

最近项目使用模型关联查询数据,因为模型关联之后返回的数据可以是一对多的数组,使用join链表查询则是返回多条数据,使用模型关联就少不了对关联的模型进行查询。

使用hasWhere 需要注意的是 haswhere只有静态方法 没有->

/**
     * 获取未完成接单列表
     * @param $user_id
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getNotList($user_id = false)
    {
        return ReceivingModel::hasWhere('demand',function($query){
            $query->where('Demand.is_status','=',0);
        })
            ->with(['user','type','demand'])
            ->where('Receiving.is_delete', '=', 0)
            ->where('Receiving.user_id','=',$user_id)
            ->order(['create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }

Demand和Receiving都是模型名称,用来区分字段


版权声明:本文为weixin_44141948原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。