使用框架再封装函数

 目的达到只输入需要的数据就可以返回需要的数据

1laravel封装条件查询

/**
     * @param array $where
     * 使用格式参考
     * [
    'where'=>['id'=>['=',100],'status'=>['<',100]],
    'orWhere'=>['id'=>'john','status'=>1],
    'whereBetween'=>['votes','1,100'],
    'whereNotBetween'=>['votes','1,100'],
    'whereIn'=>['id'=>'1,2,3'],
    whereNotIn=>['id'=>'1,2,3'],
    whereNull=>'updated_at',
    'orderBy'=>['name'=>'desc'],
    'groupBy'=>'count',
    'having'=>['count'=>['>',100]]
    'skip'=>10
    'take'=>5
    ]
     * @return mixed
     */

    static function parseWhere(array $where=[])
    {

        $o = $this;
        foreach ($where as $method=>$param)
        {
            if(is_array($param))
            {
                foreach ($param as $field=>$fieldv)
                {
                    if(is_array($fieldv))
                    {
                        if(isset($fieldv[0]) && isset($fieldv[1]))
                        {
                            $o = $o->$method($field,$fieldv[0],$fieldv[1]);
                        }

                    }
                    elseif(is_numeric($fieldv) || is_string($fieldv))
                    {
                        if(strripos($fieldv,',',0) !== false)
                        {
                            $fieldv = explode(',',$fieldv);
                        }
                        $o = $o->$method($field,$fieldv);
                    }
                }

            }
            elseif(is_numeric($param) || is_string($param))
            {
                $o = $o->$method($param);
            }

        }
        return $o;
    }
原文地址:https://www.cnblogs.com/BeautyFuture/p/6293663.html