zoukankan      html  css  js  c++  java
  • laravel 链式组合查询数据

    laravel 链式组合查询数据

    一、总结

    一句话总结:

    - 就是链式操作的基本操作,因为返回的都是一直可以进行链式操作的对象,所以我们接收返回的对象即可
    - $result = DB::table($table_name);
    - $result = $result->whereIn($first_field,$first_datas);

    二、laravel 链式组合查询数据

    转自或参考:laravel 链式组合查询数据
    https://blog.csdn.net/weixin_43885417/article/details/85018633

     

    我们写项目的时候经常用到组合查询,例如对于一篇博客,你想根据一个著作的著作类别,编著形式,研究类别进行查询。这个时候,我们就可以链式组合查询。前台给我们后台什么类型,我们就根据这个类型去执行sql语句。这样十分的灵活。
    应用场景:
    在这里插入图片描述
    下面是我封装的链式组合查询代码:

    //根据字段组合查询数据
        public static function combinationSelectDatas($condition_datas,$second_field = '',$second_datas = [],$third_field = '',$third_datas = []){
             $first_field = $condition_datas['first_field'];
             $first_datas = $condition_datas['first_datas'];
             $table_name  = $condition_datas['table_name'];
             $result = DB::table($table_name);
             if(!empty($first_datas)){
                 $result = $result->whereIn($first_field,$first_datas);
             }
             if(!empty($second_datas)){
                 $result = $result->whereIn($second_field,$second_datas);
             }
             if(!empty($third_datas)){
                 $result = $result->whereIn($third_field,$third_datas);
             }
             $result = $result->paginate($condition_datas['total']);
             $time_field  = $condition_datas['time_field'];
             foreach ($result as $datas){
                 $datas->$time_field = date('Y-m-d',$datas->$time_field/1000);
             }
             return responseTojson(0,'查询成功','',$result);
        }
    

    这样写,我们就可以随心所欲,前台给我们什么,我们就组合查什么。

     
  • 相关阅读:
    分布式系统中的Session问题
    HotSpot VM运行时---命令行选项解析
    K大数查询
    [DarkBZOJ3636] 教义问答手册
    小朋友和二叉树
    [COCI2018-2019#2] Sunčanje
    小SY的梦
    [HDU6722 & 2019百度之星初赛四 T4] 唯一指定树
    [HDU6800] Play osu! on Your Tablet
    [NOI2007] 货币兑换
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/11675189.html
Copyright © 2011-2022 走看看