php - lumen 如何执行 原生sql 语句

查看:282
本文介绍了php - lumen 如何执行 原生sql 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在lumen 中对数据库进行查询 由于条件过多判断 采取使用原生sql
如下

 public static function getAllNewsList($userId,$newId,$companyId,$communityId){
        $sql = "SELECT * FROM notices";
        $where = "WHERE addressee= :userId";
        if($newId) $where .= "or id> :newId";
        if($companyId) $where .= "or addressee= :companyId";
        if($communityId) $where .= "or addressee= :communityId";
        $query = $sql.$where;
        $res =NewNotice::select($query, ['userId' => $userId,'companyId' => $companyId,'communityId' => $communityId]);

        dd($res);
    }

但是结果不同于select(..)->get();
该如何处理啊

解决方案

    public static function getAllNewsList($userId,$newId,$companyId,$communityId){
        $res = DB::table('notices')
            ->where('addressee',$userId);
        
        if($newId){
            $res->orWhere('id','>',$newId);
        }
        if($companyId){
            $res->orWhere('addressee',$companyId);
        }
        if($communityId){
            $res->orWhere('addressee',$communityId);
        }

        $res = $res->select('*');

        dd($res);

    }

这篇关于php - lumen 如何执行 原生sql 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆