thinkphp#数据模型model返回的结果集取其数据数组
2023-03-29 23:03:03
1111
{{single.collect_count}}

用model获取数据返回的是结果集对象,有时候获取到结果集手动用cllection($ret)->toArray();也不管用。偶然发现下面方法可以更便捷只获取结果集的所查询的数据。

方法一:找到TP5框架中的database.php文件,该文件中找到 resultset_type 该项,将后面的 array 改成 \think\Collection 【注意大小写】

// 数据集返回类型'resultset_type'=> 'array', 

改为

// 数据集返回类型'resultset_type'=> '\think\Collection',

在查询数据的时候,在后面加上 ->toArray(); 例如:

$data = User::select() -> toArray();

方法二:在模型里设置
首先在Model中添加该属性:

protected $resultSetType = 'collection';
回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}