我正在与laravel vue和algolia合作。一切正常,除了分页正常。分页工作正常,但只显示fisrt页面结果。当我点击分页按钮的2,3 ...页面链接时,它不会获取下一页的结果。我所做的工作如下:
SearchController.php
public function search(Request $request)
{
$error = ['error' => 'No results found, please try with different keywords.'];
if($request->has('q')) {
$movies = Movie::search($request->get('q'))->get();
return $movies ? $movies : $error;
}
return $error;
}
Pagination.js
var search = instantsearch({
appId: 'myid',
apiKey: 'mykey',
indexName: 'myindex',
urlSync: true
});
search.addWidget(
instantsearch.widgets.pagination({
container: '#pagination-container',
maxPages: 20,
scrollTo: false
})
);
search.start();
答案 0 :(得分:4)
我认为你需要使用paginate方法将正确的页面发送到前端。
public function search(Request $request)
{
$error = ['error' => 'No results found, please try with different keywords.'];
if($request->has('q')) {
$movies = Movie::search($request->get('q'))->paginate(20)->all();
return $movies ? $movies : $error;
}
return $error;
}
如果需要,您可以明确传递页码:
Movie::search($request->get('q'))->paginate(20, 'p', 3)->all();