dr_*_*mio 2 php laravel laravel-5
我的Laravel 5.5应用程序中有很多API资源。到目前为止,它们很棒,但是在分页链接中保留URL参数时遇到了问题。
请参见下面的URL示例:/ posts?unreviewed = true
public function getPosts(Request $request){
/*
* Gets a list of posts.
*
* Options:
* - unreviewed: gets posts without revisions (default: false)
*
*/
$pagination = 20;
//Check constrains
if($request->unreviewed == true){
return SocialPostResource::collection(SocialPost::with(['images', 'publication.images'])
->doesntHave('revisions')
->paginate($pagination));
}
return SocialPostResource::collection(SocialPost::with(['images', 'publication.images'])->paginate($pagination));
}
Run Code Online (Sandbox Code Playgroud)
以下示例仅包含已修订的帖子。这在第一个查询中效果很好。问题在于分页结果在URL中不包含“ reviewed = true”参数,因此第2页及以后的页面将返回所有帖子。我需要所有URL都包括原始请求中传递的任何参数。
“data”:{...},
“links”:{
...
“next”: “/posts?page=2”
}
Run Code Online (Sandbox Code Playgroud)
我期望的结果是“ / posts?unreviewed = true&page = 2”
我遇到了同样的问题。实际上,由于您的问题,我找到了类似的帖子,这有助于我找到适合我的情况的解决方案。
请尝试以下方法:
// You must add this Facade in the 'use' section of your file:
use Illuminate\Support\Facades\Input;
return SocialPostResource::collection(
SocialPost::with(['images', 'publication.images'])
->paginate($pagination)
->appends(Input::except('page')) // <- Add this line!
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1442 次 |
| 最近记录: |