【发布时间】:2019-02-07 17:47:04
【问题描述】:
我们正在对 Spatie 的查询生成器进行一些试验,我想返回一个资源集合。
很遗憾,当您向请求添加过滤器时,这些过滤器不会作为回复的第一个、最后一个、上一个和下一个链接的一部分发回。
这是我们的代码(只是一个非常简单模式的示例ExchangeRate:
class ExchangeRateController extends Controller
{
public function index(Request $request) {
return \App\Resources\ExchangeRate::collection(
QueryBuilder::for(ExchangeRate::class)
->allowedFilters(
Filter::exact('currency'),
Filter::scope('valid-on')
)
->paginate());
}
}
当我们调用 GET /api/exchangerates 时,我们将获得 3 页,每页 15 条记录。当我们调用GET /api/exchangerates?filter[currency]=USD时,我们会得到只有 1 条记录的 1 页。这一切都很好,但是 json 响应中的链接没有得到正确的链接。
没有过滤器,我们会得到以下链接:
"links": {
"first": "https://example.com/api/exchangerates?page=1",
"last": "https://example.com/api/exchangerates?page=3",
"prev": null,
"next": "https://example.com/api/exchangerates?page=2"
}
使用过滤器,我们会在响应中获得以下链接:
"links": {
"first": "https://example.com/api/exchangerates?page=1",
"last": "https://example.com/api/exchangerates?page=1",
"prev": null,
"next": null
}
所以,它的分页是正确的,但过滤器不包含在链接中,我认为这是不正确的(客户端应该能够信任这些链接以获取 下一页它当前选择的数据集...)
有没有办法做到这一点?
【问题讨论】:
标签: json laravel api eloquent resources