spatie/Laravel-query-builder 中的 fields 过滤字段没生效

项目代码

    public function index(Request $request,Topic $topic){
        $topics = QueryBuilder::for(Topic::class)
            ->allowedIncludes('user','category','user.topics')
            ->allowedFilters( 'title',AllowedFilter::exact('category_id'),AllowedFilter::scope('withOrder')
            ->default('recentReplied'))
            ->paginate(4);

        return TopicResource::collection($topics);
    }

1.项目中已经安装了 spatie/laravel-query-builder 这个插件。
2.插件中的功能 如 include 是可以正常使用的。
2.1未使用inlcude 出现的数据

2.2使用inlcude

2.3可见 插件没有问题吧
3.使用过滤字段后

没有任何的反应
然后增加inlcude 继续选择user中的字段 ,就报错了

报错信息为!!!

    "message": "Requested field(s) `id, name` are not allowed (yet). If you want to allow these fields, please make sure to call the QueryBuilder's `allowedFields` method before the `allowedIncludes` method."

这是要指定这个allowedFields吗?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 2

已经解决 需要先指定过滤的字段 才可以进行过滤 ->allowedFields('topics.id','topics.user_id','topics.title')

3年前 评论

楼主你过滤字段是隐藏掉一些字段吗

3年前 评论
liuhaiqiang999 (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!