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吗?
已经解决 需要先指定过滤的字段 才可以进行过滤 ->allowedFields('topics.id','topics.user_id','topics.title')
楼主你过滤字段是隐藏掉一些字段吗