thinkphp5内报错Call to a member function toArray() on array
使用场景
使用模型查询后,想获取不包含对象内容的数组结构的结果。
数据库(database.php)里设置的返回数据结果为数组类型
方法一:
全局:修改Config.php
局部:在模型中加入
 
推荐,thinkphp自带的一个方法
                        
                        
                    
                    
                    
                    
                    
                    
                    
                使用场景
使用模型查询后,想获取不包含对象内容的数组结构的结果。
使用方式
$gardenAuth = new GardenAuth();
$res = $gardenAuth->where(['id'=>$id])->select()->toArray();Call to a member function toArray() on array数据库(database.php)里设置的返回数据结果为数组类型
'resultset_type' => 'array'方法一:
全局:修改Config.php
'resultset_type' => 'collection'局部:在模型中加入
protected $resultSetType = 'collection';推荐,thinkphp自带的一个方法
if (!function_exists('collection')) {
    /**
     * 数组转换为数据集对象
     * @param array $resultSet 数据集数组
     * @return \think\model\Collection|\think\Collection
     */
    function collection($resultSet)
    {
        $item = current($resultSet);
        if ($item instanceof Model) {
            return \think\model\Collection::make($resultSet);
        } else {
            return \think\Collection::make($resultSet);
        }
    }
}
 
//调用方法:
$res = collection($res)->toArray(); 
                
发表评论 取消回复