TP5.0 在控制器中根据业务隐藏多余字段

方法一

       /**
       *
       *模型查出需要数据
        /
        $products = ProductModel::getMostRecent($count);
        if(!$products){
            throw new ProductException();
        }
        /**
         *
         * 通过$product 为数据集,直接调用函数   根据业务隐藏不需要字段
         */
        $collection = collection($products);
        $products = $collection->hidden(['summary']);

方法二
TP5 默认查询出的数据未为数组,修改配置项为数据集:collection
在这里插入图片描述

public function getRecent($count=15)
    {
        //验证
        (new Count())->goCheck();
        //
        $products = ProductModel::getMostRecent($count);
        //注意数据集为空判断 用TP5自带函数 !$products 不能判断
        if(**$products->isEmpty()**){
            throw new ProductException();
        }
        /**
         *
         * 通过 collection()函数 根据业务隐藏不需要字段
         */

        $products->hidden(['summary']);
        //返回
        return $products;
    }

猜你喜欢

转载自blog.csdn.net/qq_38866543/article/details/83145429