我正在使用paginate()方法在控制器的index方法中获取分页数据,但有时我需要完整的数据,所以我编写了这样的代码。
public function index(Request $request)
{
if ($request->page === 'all') {
$posts = Post::all();
} else {
$posts = Post::paginate(10);
}
return response([
'posts' => $posts
],Response::HTTP_OK);
}
因此,如果要获取所有数据,我将页面值作为all
发送。
如果我想要分页数据,则将页面值发送为integer
。
它工作正常,但只是想知道是否还有其他更好的方法?